feat(typescript-types): Add Simplify<T, E, I>
This commit is contained in:
@@ -6,6 +6,18 @@ A utility types library for Typescript.
|
||||
|
||||
The types included in this library are categorized by their purpose.
|
||||
|
||||
#### Definition Types
|
||||
|
||||
| Type | Description |
|
||||
| ---------------------- | -------------------------------------------------------------------- |
|
||||
| [`Primitive`][] | A union of the primitive types of TypeScript. |
|
||||
| [`BuiltIns`][] | A union of the built-in types of TypeScript. |
|
||||
| [`NonContainerType`][] | A union of types, whose primary pourpose is not holding other types. |
|
||||
|
||||
[`Primitive`]: src/primitive.ts
|
||||
[`BuiltIns`]: src/built-ins.ts
|
||||
[`NonContainerType`]: src/non-container-type.ts
|
||||
|
||||
#### Test Types
|
||||
|
||||
| Type | Description |
|
||||
@@ -36,9 +48,12 @@ The types included in this library are categorized by their purpose.
|
||||
|
||||
#### Conversion Types
|
||||
|
||||
| Type | Description |
|
||||
| --------------------------------- | ------------------------------------------------------------------ |
|
||||
| [`KeyPath<T, Separator = '.'>`][] | Converts a `Separator` separated string into a tuple of its parts. |
|
||||
| Type | Description |
|
||||
| --------------------------------- | ---------------------------------------------------------------------------------------------------- |
|
||||
| [`KeyPath<T, Separator = '.'>`][] | Converts a `Separator` separated string into a tuple of its parts. |
|
||||
| [`Simplify<T, E, I>`][] | Maps types that extend `I` and not `E` to their properties. Helps diagnostic messages to be clearer. |
|
||||
|
||||
[`Simplify<T, E, I>`]: src/simplify.ts
|
||||
|
||||
#### Combination Types
|
||||
|
||||
|
||||
3
packages/typescript-types/src/built-ins.ts
Normal file
3
packages/typescript-types/src/built-ins.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import type { Primitive } from './primitive.js'
|
||||
|
||||
export type BuiltIns = Primitive | void | Date | RegExp
|
||||
3
packages/typescript-types/src/non-container-type.ts
Normal file
3
packages/typescript-types/src/non-container-type.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import type { BuiltIns } from './built-ins.js'
|
||||
|
||||
export type NonContainerType = BuiltIns | Function | (new (...arguments_: any[]) => unknown)
|
||||
1
packages/typescript-types/src/primitive.ts
Normal file
1
packages/typescript-types/src/primitive.ts
Normal file
@@ -0,0 +1 @@
|
||||
export type Primitive = null | undefined | string | number | boolean | symbol | bigint | PropertyKey
|
||||
9
packages/typescript-types/src/simplify.ts
Normal file
9
packages/typescript-types/src/simplify.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import type { NonContainerType } from './non-container-type.js'
|
||||
|
||||
export type Simplify<T, Exclude = NonContainerType, Include = object> = T extends Exclude
|
||||
? T
|
||||
: T extends Include
|
||||
? {
|
||||
[K in keyof T]: Simplify<T[K], Exclude, Include>
|
||||
}
|
||||
: T
|
||||
44
packages/typescript-types/test/simplify.tst.ts
Normal file
44
packages/typescript-types/test/simplify.tst.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import type { Simplify } from '@/simplify.js'
|
||||
import { expect } from 'tstyche'
|
||||
|
||||
type TyreSize = {
|
||||
radius: number
|
||||
type: 'winter' | 'summer'
|
||||
}
|
||||
|
||||
type Maker = {
|
||||
maker: string
|
||||
}
|
||||
|
||||
type TyreMaker = Maker & TyreSize
|
||||
|
||||
type SimplifiedTyreMaker = {
|
||||
radius: number
|
||||
type: 'winter' | 'summer'
|
||||
maker: string
|
||||
}
|
||||
|
||||
expect<TyreMaker>().type.not.toBe<SimplifiedTyreMaker>()
|
||||
expect<Simplify<TyreMaker>>().type.toBe<SimplifiedTyreMaker>()
|
||||
|
||||
type Train = {
|
||||
wheels: TyreMaker[]
|
||||
}
|
||||
|
||||
type SimplifiedTrain = {
|
||||
wheels: SimplifiedTyreMaker[]
|
||||
}
|
||||
|
||||
expect<Train>().type.not.toBe<SimplifiedTrain>()
|
||||
expect<Simplify<Train>>().type.toBe<SimplifiedTrain>()
|
||||
|
||||
type Car = {
|
||||
wheels: [TyreMaker, TyreMaker]
|
||||
}
|
||||
|
||||
type SimplifiedCar = {
|
||||
wheels: [SimplifiedTyreMaker, SimplifiedTyreMaker]
|
||||
}
|
||||
|
||||
expect<Car>().type.not.toBe<SimplifiedCar>()
|
||||
expect<Simplify<Car>>().type.toBe<SimplifiedCar>()
|
||||
Reference in New Issue
Block a user