feat(typescript-types): Add Simplify<T, E, I>

This commit is contained in:
T. R. Bernstein
2025-07-15 11:19:43 +02:00
parent 9700b7d46d
commit 2aa71d91e4
6 changed files with 78 additions and 3 deletions

View File

@@ -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

View File

@@ -0,0 +1,3 @@
import type { Primitive } from './primitive.js'
export type BuiltIns = Primitive | void | Date | RegExp

View File

@@ -0,0 +1,3 @@
import type { BuiltIns } from './built-ins.js'
export type NonContainerType = BuiltIns | Function | (new (...arguments_: any[]) => unknown)

View File

@@ -0,0 +1 @@
export type Primitive = null | undefined | string | number | boolean | symbol | bigint | PropertyKey

View 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

View 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>()