@kakasoo/deep-strict-types
Advanced tools
Comparing version 1.0.0 to 1.0.1
type Primitive = string | number | boolean | symbol | null | undefined; | ||
type Unbrand<T extends Primitive & NonNullable<unknown>> = T extends Primitive & NonNullable<unknown> ? Extract<Primitive, T> : never; | ||
type Unbrand<T extends Primitive & Record<any, any>> = T extends string & Record<any, any> ? Extract<string, Omit<T, any>> : T extends number & Record<any, any> ? Extract<number, Omit<T, any>> : T extends boolean & Record<any, any> ? Extract<boolean, Omit<T, any>> : T extends symbol & Record<any, any> ? Extract<symbol, Omit<T, any>> : T extends null & Record<any, any> ? Extract<null, Omit<T, any>> : T extends undefined & Record<any, any> ? Extract<undefined, Omit<T, any>> : T; | ||
export type DeepStrictUnbrand<T> = T extends Array<Date> ? Array<Date> : T extends Array<infer I extends object> ? Array<DeepStrictUnbrand<I>> : T extends Primitive & NonNullable<unknown> ? Unbrand<T> : T extends Date ? T : { | ||
[K in keyof T]: T[K] extends Unbrand<infer I> ? Extract<I, T[K]> : T[K] extends object ? DeepStrictUnbrand<T[K]> : T[K]; | ||
[K in keyof T]: T[K] extends object ? DeepStrictUnbrand<T[K]> : T[K]; | ||
}; | ||
export {}; | ||
//# sourceMappingURL=DeepStrictUnbrand.d.ts.map |
{ | ||
"name": "@kakasoo/deep-strict-types", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "", | ||
@@ -18,3 +18,3 @@ "private": false, | ||
"prepare": "ts-patch install && typia patch", | ||
"test": "node --watch bin/test/*.js" | ||
"test": "node bin/test/index.js" | ||
}, | ||
@@ -21,0 +21,0 @@ "keywords": [], |
@@ -1,1 +0,90 @@ | ||
# DeepStrictTypes | ||
# DeepStrictTypes | ||
**DeepStrictTypes** extends TypeScript utility types, enabling safe operations like `Omit` and `Pick` on nested objects or arrays by specifying the keys to be inferred. This allows for more strict and accurate type checks. | ||
## DeepStrictObjectKeys | ||
`DeepStrictObjectKeys<T>` extracts all nested keys from an object `T`, preserving the structure of the nested object and returning the types of the keys. This is useful when you need to handle specific keys safely at deeper levels of an object. | ||
```typescript | ||
type Example = { | ||
user: { | ||
name: string; | ||
address: { | ||
city: string; | ||
zip: number; | ||
}; | ||
}; | ||
}; | ||
// Result: "user" | "user.name" | "user.address" | "user.address.city" | "user.address.zip" | ||
type Keys = DeepStrictObjectKeys<Example>; | ||
``` | ||
## DeepStrictOmit | ||
DeepStrictOmit<T, K> creates a new type by excluding properties corresponding to the key K from object T, while preserving the nested structure. This type allows precise omission of keys even in deeply nested objects. | ||
```ts | ||
type Example = { | ||
user: { | ||
name: string; | ||
age: number; | ||
}; | ||
}; | ||
// Result: { user: { age: number; } } | ||
type Omitted = DeepStrictOmit<Example, "user.name">; | ||
``` | ||
## DeepStrictPick | ||
DeepStrictPick<T, K> creates a new type by selecting only the properties corresponding to the key K from object T, while preserving the nested structure. It allows safely selecting specific keys even from deep objects. | ||
```ts | ||
type Example = { | ||
user: { | ||
name: string; | ||
age: number; | ||
}; | ||
}; | ||
// Result: { user: { name: string; } } | ||
type Picked = DeepStrictPick<Example, "user.name">; | ||
``` | ||
## DeepStrictUnbrand | ||
DeepStrictUnbrand<T> removes branding from type T and applies it even to deeply nested objects. This makes handling complex branded types simpler by removing the branding for more straightforward use. | ||
```ts | ||
type BrandedType = { brand: number & { type: "won" } }; | ||
// Result: { value: number; } | ||
type Unbranded = DeepStrictUnbrand<BrandedType>; | ||
``` | ||
## SubTypes for implementation | ||
### ElementOf | ||
ElementOf<T> extracts the type of elements from an array type T. This is useful to explicitly define the element type of an array and perform operations on that element. | ||
```ts | ||
type ArrayExample = string[]; | ||
// Result: string | ||
type ElementType = ElementOf<ArrayExample>; | ||
``` | ||
### Equal | ||
Equal<A, B> evaluates whether types A and B are the same and returns true or false. This is used to validate whether two types are identical. | ||
```ts | ||
type A = { a: number }; | ||
type B = { a: number }; | ||
// Result: true | ||
type AreEqual = Equal<A, B>; | ||
``` |
Sorry, the diff of this file is not supported yet
25178
165
91