-
-
Notifications
You must be signed in to change notification settings - Fork 603
export ConditionalSimplify
and ConditionalSimplifyDeep
#1094
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,63 @@ | ||||||||
/** | ||||||||
Recursively simplifies a type while including and/or excluding certain types from being simplified. | ||||||||
|
||||||||
@example | ||||||||
```ts | ||||||||
import type {ConditionalSimplifyDeep} from 'type-fest'; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Etc etc. |
||||||||
type TypeA = { | ||||||||
foo: { | ||||||||
a: string | ||||||||
} | ||||||||
}; | ||||||||
type TypeB = { | ||||||||
foo: { | ||||||||
b: string | ||||||||
} | ||||||||
}; | ||||||||
type SimplifyDeepTypeAB = ConditionalSimplifyDeep<TypeA & TypeB, never, object>; | ||||||||
//=> {foo: {a: string, b: string}} | ||||||||
``` | ||||||||
@example | ||||||||
```ts | ||||||||
import type {ConditionalSimplifyDeep} from 'type-fest'; | ||||||||
type SomeComplexType1 = { | ||||||||
a1: string | ||||||||
b1: number | ||||||||
c1: boolean | ||||||||
... | ||||||||
} | ||||||||
type SomeComplexType2 = { | ||||||||
a2: string | ||||||||
b2: number | ||||||||
c2: boolean | ||||||||
... | ||||||||
} | ||||||||
type TypeA = { | ||||||||
foo: { | ||||||||
a: string | ||||||||
complexType: SomeComplexType1 | ||||||||
} | ||||||||
}; | ||||||||
type TypeB = { | ||||||||
foo: { | ||||||||
b: string | ||||||||
complexType: SomeComplexType2 | ||||||||
} | ||||||||
}; | ||||||||
type SimplifyDeepTypeAB = ConditionalSimplifyDeep<TypeA & TypeB, SomeComplexType1 | SomeComplexType2, object>; | ||||||||
//=> { | ||||||||
// foo: { | ||||||||
// a: string, | ||||||||
// b: string, | ||||||||
// complexType: SomeComplexType1 | SomeComplexType2 | ||||||||
// } | ||||||||
// } | ||||||||
``` | ||||||||
@see SimplifyDeep | ||||||||
@category Object | ||||||||
*/ | ||||||||
export type ConditionalSimplifyDeep<Type, ExcludeType = never, IncludeType = unknown> = Type extends ExcludeType | ||||||||
? Type | ||||||||
: Type extends IncludeType | ||||||||
? {[TypeKey in keyof Type]: ConditionalSimplifyDeep<Type[TypeKey], ExcludeType, IncludeType>} | ||||||||
: Type; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,36 @@ | ||
/** | ||
Simplifies a type while including and/or excluding certain types from being simplified. Useful to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability. | ||
|
||
This type is **experimental** and was introduced as a result of this {@link https://github.com/sindresorhus/type-fest/issues/436 issue}. It should be used with caution. | ||
|
||
@internal | ||
@experimental | ||
@see Simplify | ||
Simplifies a type while including and/or excluding certain types from being simplified. | ||
Useful to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability. | ||
@example | ||
```ts | ||
import type {ConditionalSimplify} from 'type-fest'; | ||
type TypeA = { | ||
a: string | ||
} | ||
type TypeB = { | ||
b: string | ||
} | ||
type TypeAB = TypeA & TypeB | ||
//=> {a: string} & {b: string} | ||
type SimplifyTypeAB = ConditionalSimplify<TypeAB, never, object> | ||
//=> {a: string, b: string} | ||
``` | ||
@example | ||
```ts | ||
import type {ConditionalSimplify} from 'type-fest'; | ||
type Simplify<T> = ConditionalSimplify<T, Set<unknown> | Map<unknown, unknown> | unknown[], object>; | ||
type A = Simplify<Set<number> & Set<string>>; | ||
//=> Set<number> & Set<string> | ||
type B = Simplify<Map<number, number> & Map<string, string>>; | ||
//=> Map<number, number> & Map<string, string> | ||
type C = Simplify<{ a: number } & { b: string }>; | ||
//=> {a: number, b: string} | ||
``` | ||
@see ConditionalSimplifyDeep | ||
@category Object | ||
*/ | ||
export type ConditionalSimplify<Type, ExcludeType = never, IncludeType = unknown> = Type extends ExcludeType | ||
? Type | ||
: Type extends IncludeType | ||
? {[TypeKey in keyof Type]: Type[TypeKey]} | ||
: Type; | ||
|
||
/** | ||
Recursively simplifies a type while including and/or excluding certain types from being simplified. | ||
|
||
This type is **experimental** and was introduced as a result of this {@link https://github.com/sindresorhus/type-fest/issues/436 issue}. It should be used with caution. | ||
|
||
See {@link ConditionalSimplify} for usages and examples. | ||
|
||
@internal | ||
@experimental | ||
@category Object | ||
*/ | ||
export type ConditionalSimplifyDeep<Type, ExcludeType = never, IncludeType = unknown> = Type extends ExcludeType | ||
? Type | ||
: Type extends IncludeType | ||
? {[TypeKey in keyof Type]: ConditionalSimplifyDeep<Type[TypeKey], ExcludeType, IncludeType>} | ||
: Type; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import {expectNotAssignable, expectType} from 'tsd'; | ||
import type {ConditionalSimplifyDeep} from '../index'; | ||
|
||
type Position = {top: number; left: number}; | ||
type Size = {width: number; height: number}; | ||
|
||
// In your editor, hovering over `PositionAndSizeSimplified` will show a simplified object with all the properties. | ||
type PositionAndSizeIntersection = Position & Size; | ||
|
||
const position = {top: 120, left: 240}; | ||
const size = {width: 480, height: 600}; | ||
const positionAndSize = {...position, ...size}; | ||
|
||
// Should simplify interface deeply. | ||
type SomeNode = { | ||
parent: PositionAndSizeIntersection; | ||
childs: Array<{parent: PositionAndSizeIntersection}>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider renaming 'childs' to 'children' for correct plural form. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
}; | ||
|
||
// In your editor, hovering over `SomeNodeSimplified` will show a simplified object with all the properties. | ||
type SomeNodeSimplified = ConditionalSimplifyDeep<SomeNode>; | ||
|
||
const someNode = {parent: positionAndSize, childs: [{parent: positionAndSize}, {parent: positionAndSize}]}; | ||
expectType<SomeNodeSimplified>(someNode); | ||
|
||
// Should simplify interface deeply excluding Function type. | ||
// TODO: Convert this to a `type`. | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions | ||
interface MovablePosition extends Position { | ||
move(position: Position): Position; | ||
} | ||
|
||
type MovableCollection = { | ||
position: MovablePosition; | ||
top: {position: MovablePosition; size: Size}; | ||
left: {position: MovablePosition; size: Size}; | ||
}; | ||
|
||
type MovableNodeSimplifiedFail = ConditionalSimplifyDeep<MovableCollection>; | ||
type MovableNodeSimplifiedPass = ConditionalSimplifyDeep<MovableCollection, Function>; | ||
|
||
declare const movableNodeSimplifiedFail: MovableNodeSimplifiedFail; | ||
declare const movableNodeSimplifiedPass: MovableNodeSimplifiedPass; | ||
|
||
expectNotAssignable<MovableCollection>(movableNodeSimplifiedFail); | ||
expectType<MovableCollection>(movableNodeSimplifiedPass); | ||
|
||
const movablePosition = { | ||
top: 42, | ||
left: 42, | ||
move(position: Position) { | ||
return position; | ||
}, | ||
}; | ||
|
||
const movableNode = { | ||
position: movablePosition, | ||
top: {position: movablePosition, size}, | ||
left: {position: movablePosition, size}, | ||
}; | ||
|
||
expectType<MovableNodeSimplifiedPass>(movableNode); | ||
|
||
// Should exclude `Function` and `Size` type (mainly visual, mouse over the statement). | ||
type ExcludeFunctionAndSize1 = ConditionalSimplifyDeep<MovableCollection, Function | Size>; | ||
expectType<ExcludeFunctionAndSize1>(movableNode); | ||
|
||
// Same as above but using `IncludeType` parameter (mainly visual, mouse over the statement). | ||
type ExcludeFunctionAndSize2 = ConditionalSimplifyDeep<MovableCollection, Function, MovableCollection | Position>; | ||
expectType<ExcludeFunctionAndSize2>(movableNode); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,20 @@ | ||
import {expectNotAssignable, expectType} from 'tsd'; | ||
import type {ConditionalSimplify, ConditionalSimplifyDeep} from '../source/conditional-simplify'; | ||
import type {ConditionalSimplify} from '../index'; | ||
|
||
type Position = {top: number; left: number}; | ||
type Size = {width: number; height: number}; | ||
|
||
// In your editor, hovering over `PositionAndSizeSimplified` will show a simplified object with all the properties. | ||
type PositionAndSizeIntersection = Position & Size; | ||
type PositionAndSizeSimplified = ConditionalSimplify<PositionAndSizeIntersection>; | ||
|
||
const position = {top: 120, left: 240}; | ||
const size = {width: 480, height: 600}; | ||
const positionAndSize = {...position, ...size}; | ||
expectType<PositionAndSizeSimplified>(positionAndSize); | ||
|
||
// Exclude function type to be simplified. | ||
type SomeFunction = (type: string) => string; | ||
type SimplifiedFunctionFail = ConditionalSimplify<SomeFunction>; // Return '{}' | ||
type SimplifiedFunctionPass = ConditionalSimplify<SomeFunction, Function>; // Return '(type: string) => string' | ||
|
||
declare const simplifiedFunctionFail: SimplifiedFunctionFail; | ||
declare const simplifiedFunctionPass: SimplifiedFunctionPass; | ||
|
||
expectNotAssignable<SomeFunction>(simplifiedFunctionFail); | ||
expectType<SomeFunction>(simplifiedFunctionPass); | ||
|
||
// Should simplify interface deeply. | ||
type SomeNode = { | ||
parent: PositionAndSizeIntersection; | ||
childs: Array<{parent: PositionAndSizeIntersection}>; | ||
}; | ||
|
||
// In your editor, hovering over `SomeNodeSimplified` will show a simplified object with all the properties. | ||
type SomeNodeSimplified = ConditionalSimplifyDeep<SomeNode>; | ||
|
||
const someNode = {parent: positionAndSize, childs: [{parent: positionAndSize}, {parent: positionAndSize}]}; | ||
expectType<SomeNodeSimplified>(someNode); | ||
|
||
// Should simplify interface deeply excluding Function type. | ||
// TODO: Convert this to a `type`. | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions | ||
interface MovablePosition extends Position { | ||
move(position: Position): Position; | ||
} | ||
|
||
type MovableCollection = { | ||
position: MovablePosition; | ||
top: {position: MovablePosition; size: Size}; | ||
left: {position: MovablePosition; size: Size}; | ||
}; | ||
|
||
type MovableNodeSimplifiedFail = ConditionalSimplifyDeep<MovableCollection>; | ||
type MovableNodeSimplifiedPass = ConditionalSimplifyDeep<MovableCollection, Function>; | ||
|
||
declare const movableNodeSimplifiedFail: MovableNodeSimplifiedFail; | ||
declare const movableNodeSimplifiedPass: MovableNodeSimplifiedPass; | ||
|
||
expectNotAssignable<MovableCollection>(movableNodeSimplifiedFail); | ||
expectType<MovableCollection>(movableNodeSimplifiedPass); | ||
|
||
const movablePosition = { | ||
top: 42, | ||
left: 42, | ||
move(position: Position) { | ||
return position; | ||
}, | ||
}; | ||
|
||
const movableNode = { | ||
position: movablePosition, | ||
top: {position: movablePosition, size}, | ||
left: {position: movablePosition, size}, | ||
}; | ||
|
||
expectType<MovableNodeSimplifiedPass>(movableNode); | ||
|
||
// Should exclude `Function` and `Size` type (mainly visual, mouse over the statement). | ||
type ExcludeFunctionAndSize1 = ConditionalSimplifyDeep<MovableCollection, Function | Size>; | ||
expectType<ExcludeFunctionAndSize1>(movableNode); | ||
|
||
// Same as above but using `IncludeType` parameter (mainly visual, mouse over the statement). | ||
type ExcludeFunctionAndSize2 = ConditionalSimplifyDeep<MovableCollection, Function, MovableCollection | Position>; | ||
expectType<ExcludeFunctionAndSize2>(movableNode); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please preserve the code example like it was originally, this includes empty lines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@romanywu I was talking in general, not just this example. All the code.