-
-
Notifications
You must be signed in to change notification settings - Fork 613
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
30b79ef
export `ConditionalSimplify` and `ConditionalSimplifyDeep`
romanywu d690115
Update conditional-simplify-deep.d.ts
romanywu 1dac47c
Update conditional-simplify-deep.d.ts
romanywu 7dda870
Update conditional-simplify-deep.d.ts
romanywu 53e39b3
Update conditional-simplify.d.ts
romanywu be8adf0
Update conditional-simplify.ts
romanywu 34718d3
Update conditional-simplify.ts
romanywu a3ef8cf
Merge branch 'main' into bug_fix_1005
romanywu c04fb38
Update merge-deep.d.ts
romanywu 3e25236
Merge branch 'main' into bug_fix_1005
sindresorhus 2d19f42
Update index.d.ts
romanywu 9a1f0f2
Update conditional-simplify-deep.ts
romanywu a76e4cb
Update conditional-simplify-deep.d.ts
sindresorhus 1a4f47d
Update conditional-simplify.d.ts
sindresorhus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
Recursively simplifies a type while including and/or excluding certain types from being simplified. | ||
|
||
@example | ||
``` | ||
import type {ConditionalSimplifyDeep} from 'type-fest'; | ||
romanywu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
type TypeA = { | ||
foo: { | ||
a: string | ||
} | ||
}; | ||
|
||
type TypeB = { | ||
foo: { | ||
b: string | ||
} | ||
}; | ||
|
||
type SimplifyDeepTypeAB = ConditionalSimplifyDeep<TypeA & TypeB, never, object>; | ||
//=> {foo: {a: string, b: string}} | ||
``` | ||
|
||
@example | ||
``` | ||
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,48 @@ | ||
/** | ||
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. | ||
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. | ||
Useful to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability. | ||
|
||
@internal | ||
@experimental | ||
@see Simplify | ||
@category Object | ||
*/ | ||
export type ConditionalSimplify<Type, ExcludeType = never, IncludeType = unknown> = Type extends ExcludeType | ||
? Type | ||
: Type extends IncludeType | ||
? {[TypeKey in keyof Type]: Type[TypeKey]} | ||
: Type; | ||
@example | ||
``` | ||
import type {ConditionalSimplify} from 'type-fest'; | ||
|
||
/** | ||
Recursively simplifies a type while including and/or excluding certain types from being simplified. | ||
type TypeA = { | ||
a: string | ||
}; | ||
|
||
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. | ||
type TypeB = { | ||
b: string | ||
}; | ||
|
||
See {@link ConditionalSimplify} for usages and examples. | ||
type TypeAB = TypeA & TypeB; | ||
//=> {a: string} & {b: string} | ||
|
||
@internal | ||
@experimental | ||
type SimplifyTypeAB = ConditionalSimplify<TypeAB, never, object>; | ||
//=> {a: string, b: string} | ||
``` | ||
|
||
@example | ||
``` | ||
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 ConditionalSimplifyDeep<Type, ExcludeType = never, IncludeType = unknown> = Type extends ExcludeType | ||
export type ConditionalSimplify<Type, ExcludeType = never, IncludeType = unknown> = Type extends ExcludeType | ||
? Type | ||
: Type extends IncludeType | ||
? {[TypeKey in keyof Type]: ConditionalSimplifyDeep<Type[TypeKey], ExcludeType, IncludeType>} | ||
? {[TypeKey in keyof Type]: Type[TypeKey]} | ||
: Type; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import {expectNotAssignable, expectType} from 'tsd'; | ||
import type {ConditionalSimplifyDeep} from '../index.d.ts'; | ||
|
||
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.