-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Add numeric constraints to type parameters of mapped types with narrowed down array constraints #55386
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
Draft
Andarist
wants to merge
6
commits into
microsoft:main
Choose a base branch
from
Andarist:fix/indexed-access-on-array-constraint-from-substitution
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add numeric constraints to type parameters of mapped types with narrowed down array constraints #55386
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2fe8d24
Add numeric constraints to type parameters of mapped types with narro…
Andarist c741755
Merge remote-tracking branch 'origin/main' into fix/indexed-access-on…
Andarist 5e59248
Merge remote-tracking branch 'origin/main' into fix/indexed-access-on…
Andarist 3cbbaed
Merge remote-tracking branch 'origin/main' into fix/indexed-access-on…
Andarist 8b45731
update baselines
Andarist 563ca76
Merge remote-tracking branch 'origin/main' into fix/indexed-access-on…
Andarist 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
76 changes: 76 additions & 0 deletions
76
tests/baselines/reference/numericStringLiteralTypes2.symbols
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,76 @@ | ||
//// [tests/cases/conformance/types/literal/numericStringLiteralTypes2.ts] //// | ||
|
||
=== numericStringLiteralTypes2.ts === | ||
// https://github.com/microsoft/TypeScript/issues/55383 | ||
|
||
type LiteralType = string | number | boolean; | ||
>LiteralType : Symbol(LiteralType, Decl(numericStringLiteralTypes2.ts, 0, 0)) | ||
|
||
type ValueGetter<ValueType extends LiteralType = LiteralType> = () => ValueType; | ||
>ValueGetter : Symbol(ValueGetter, Decl(numericStringLiteralTypes2.ts, 2, 45)) | ||
>ValueType : Symbol(ValueType, Decl(numericStringLiteralTypes2.ts, 3, 17)) | ||
>LiteralType : Symbol(LiteralType, Decl(numericStringLiteralTypes2.ts, 0, 0)) | ||
>LiteralType : Symbol(LiteralType, Decl(numericStringLiteralTypes2.ts, 0, 0)) | ||
>ValueType : Symbol(ValueType, Decl(numericStringLiteralTypes2.ts, 3, 17)) | ||
|
||
type Schema = SchemaArray | SchemaObject | LiteralType; | ||
>Schema : Symbol(Schema, Decl(numericStringLiteralTypes2.ts, 3, 80)) | ||
>SchemaArray : Symbol(SchemaArray, Decl(numericStringLiteralTypes2.ts, 5, 55)) | ||
>SchemaObject : Symbol(SchemaObject, Decl(numericStringLiteralTypes2.ts, 6, 67)) | ||
>LiteralType : Symbol(LiteralType, Decl(numericStringLiteralTypes2.ts, 0, 0)) | ||
|
||
type SchemaArray = Array<SchemaObject | SchemaArray | LiteralType>; | ||
>SchemaArray : Symbol(SchemaArray, Decl(numericStringLiteralTypes2.ts, 5, 55)) | ||
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>SchemaObject : Symbol(SchemaObject, Decl(numericStringLiteralTypes2.ts, 6, 67)) | ||
>SchemaArray : Symbol(SchemaArray, Decl(numericStringLiteralTypes2.ts, 5, 55)) | ||
>LiteralType : Symbol(LiteralType, Decl(numericStringLiteralTypes2.ts, 0, 0)) | ||
|
||
type SchemaObject = { | ||
>SchemaObject : Symbol(SchemaObject, Decl(numericStringLiteralTypes2.ts, 6, 67)) | ||
|
||
[key: string]: SchemaObject | SchemaArray | LiteralType; | ||
>key : Symbol(key, Decl(numericStringLiteralTypes2.ts, 8, 3)) | ||
>SchemaObject : Symbol(SchemaObject, Decl(numericStringLiteralTypes2.ts, 6, 67)) | ||
>SchemaArray : Symbol(SchemaArray, Decl(numericStringLiteralTypes2.ts, 5, 55)) | ||
>LiteralType : Symbol(LiteralType, Decl(numericStringLiteralTypes2.ts, 0, 0)) | ||
|
||
}; | ||
|
||
type InferValuesFromSchema<S extends Schema> = S extends LiteralType | ||
>InferValuesFromSchema : Symbol(InferValuesFromSchema, Decl(numericStringLiteralTypes2.ts, 9, 2)) | ||
>S : Symbol(S, Decl(numericStringLiteralTypes2.ts, 11, 27)) | ||
>Schema : Symbol(Schema, Decl(numericStringLiteralTypes2.ts, 3, 80)) | ||
>S : Symbol(S, Decl(numericStringLiteralTypes2.ts, 11, 27)) | ||
>LiteralType : Symbol(LiteralType, Decl(numericStringLiteralTypes2.ts, 0, 0)) | ||
|
||
? ValueGetter<S> | ||
>ValueGetter : Symbol(ValueGetter, Decl(numericStringLiteralTypes2.ts, 2, 45)) | ||
>S : Symbol(S, Decl(numericStringLiteralTypes2.ts, 11, 27)) | ||
|
||
: S extends SchemaArray | ||
>S : Symbol(S, Decl(numericStringLiteralTypes2.ts, 11, 27)) | ||
>SchemaArray : Symbol(SchemaArray, Decl(numericStringLiteralTypes2.ts, 5, 55)) | ||
|
||
? { | ||
[K in keyof S]: InferValuesFromSchema<S[K]>; // `S[K]` should satisfy the required `Schema` constraint here | ||
>K : Symbol(K, Decl(numericStringLiteralTypes2.ts, 15, 7)) | ||
>S : Symbol(S, Decl(numericStringLiteralTypes2.ts, 11, 27)) | ||
>InferValuesFromSchema : Symbol(InferValuesFromSchema, Decl(numericStringLiteralTypes2.ts, 9, 2)) | ||
>S : Symbol(S, Decl(numericStringLiteralTypes2.ts, 11, 27)) | ||
>K : Symbol(K, Decl(numericStringLiteralTypes2.ts, 15, 7)) | ||
} | ||
: S extends SchemaObject | ||
>S : Symbol(S, Decl(numericStringLiteralTypes2.ts, 11, 27)) | ||
>SchemaObject : Symbol(SchemaObject, Decl(numericStringLiteralTypes2.ts, 6, 67)) | ||
|
||
? { | ||
[K in keyof S]: InferValuesFromSchema<S[K]>; | ||
>K : Symbol(K, Decl(numericStringLiteralTypes2.ts, 19, 7)) | ||
>S : Symbol(S, Decl(numericStringLiteralTypes2.ts, 11, 27)) | ||
>InferValuesFromSchema : Symbol(InferValuesFromSchema, Decl(numericStringLiteralTypes2.ts, 9, 2)) | ||
>S : Symbol(S, Decl(numericStringLiteralTypes2.ts, 11, 27)) | ||
>K : Symbol(K, Decl(numericStringLiteralTypes2.ts, 19, 7)) | ||
} | ||
: never; | ||
|
46 changes: 46 additions & 0 deletions
46
tests/baselines/reference/numericStringLiteralTypes2.types
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,46 @@ | ||
//// [tests/cases/conformance/types/literal/numericStringLiteralTypes2.ts] //// | ||
|
||
=== numericStringLiteralTypes2.ts === | ||
// https://github.com/microsoft/TypeScript/issues/55383 | ||
|
||
type LiteralType = string | number | boolean; | ||
>LiteralType : LiteralType | ||
> : ^^^^^^^^^^^ | ||
|
||
type ValueGetter<ValueType extends LiteralType = LiteralType> = () => ValueType; | ||
>ValueGetter : ValueGetter<ValueType> | ||
> : ^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
type Schema = SchemaArray | SchemaObject | LiteralType; | ||
>Schema : Schema | ||
> : ^^^^^^ | ||
|
||
type SchemaArray = Array<SchemaObject | SchemaArray | LiteralType>; | ||
>SchemaArray : SchemaArray | ||
> : ^^^^^^^^^^^ | ||
|
||
type SchemaObject = { | ||
>SchemaObject : SchemaObject | ||
> : ^^^^^^^^^^^^ | ||
|
||
[key: string]: SchemaObject | SchemaArray | LiteralType; | ||
>key : string | ||
> : ^^^^^^ | ||
|
||
}; | ||
|
||
type InferValuesFromSchema<S extends Schema> = S extends LiteralType | ||
>InferValuesFromSchema : InferValuesFromSchema<S> | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
? ValueGetter<S> | ||
: S extends SchemaArray | ||
? { | ||
[K in keyof S]: InferValuesFromSchema<S[K]>; // `S[K]` should satisfy the required `Schema` constraint here | ||
} | ||
: S extends SchemaObject | ||
? { | ||
[K in keyof S]: InferValuesFromSchema<S[K]>; | ||
} | ||
: never; | ||
|
25 changes: 25 additions & 0 deletions
25
tests/cases/conformance/types/literal/numericStringLiteralTypes2.ts
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,25 @@ | ||
// @strict: true | ||
// @noEmit: true | ||
|
||
// https://github.com/microsoft/TypeScript/issues/55383 | ||
|
||
type LiteralType = string | number | boolean; | ||
type ValueGetter<ValueType extends LiteralType = LiteralType> = () => ValueType; | ||
|
||
type Schema = SchemaArray | SchemaObject | LiteralType; | ||
type SchemaArray = Array<SchemaObject | SchemaArray | LiteralType>; | ||
type SchemaObject = { | ||
[key: string]: SchemaObject | SchemaArray | LiteralType; | ||
}; | ||
|
||
type InferValuesFromSchema<S extends Schema> = S extends LiteralType | ||
? ValueGetter<S> | ||
: S extends SchemaArray | ||
? { | ||
[K in keyof S]: InferValuesFromSchema<S[K]>; // `S[K]` should satisfy the required `Schema` constraint here | ||
} | ||
: S extends SchemaObject | ||
? { | ||
[K in keyof S]: InferValuesFromSchema<S[K]>; | ||
} | ||
: never; |
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.
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.
I will note that this call is at least a bit cursed;
getConditionalFlowTypeOfType
returnsType
, notTypeParameter
.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.
yeee, this is really cursed 😬 since the result of this function was accepted as a valid type to
getConstraintOfTypeParameter
... I didn't really recheck what's happening here.I'm changing this to a draft as I also noticed now that it needs extra consideration as to how it interacts with
getResolvedApparentTypeOfMappedType
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.
I'm not yet sure what to do about it, especially in light of #57801 that only treats mapped types ar arrays/tuples when every constituent of an intersection is array/tuple.
In this situation the constraint of the substitution type created by
getConditionalFlowTypeOfType
is smth likeSchema & SchemaArray
(so(SchemaArray | SchemaObject | LiteralType) & SchemaArray
). The user's intention is clearly to treat the narrowed-down type as an array/tuple but this intersection type doesn't quite guarantee us that. Even though often an intersection like this is meant to "filter" it isn't exactly that.I'll have to think about this one more but in the meantime I realized that there is a bug here that can be fixed separately: #60084