Skip to content

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
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16633,7 +16633,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (getTypeParameterFromMappedType(mappedType) === getActualTypeVariable(type)) {
const typeParameter = getHomomorphicTypeVariable(mappedType);
if (typeParameter) {
const constraint = getConstraintOfTypeParameter(typeParameter);
const constraint = getConstraintOfTypeParameter(getConditionalFlowTypeOfType(typeParameter, parent));
Copy link
Member

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 returns Type, not TypeParameter.

Copy link
Contributor Author

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

Copy link
Contributor Author

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 like Schema & 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

if (constraint && everyType(constraint, isArrayOrTupleType)) {
constraints = append(constraints, getUnionType([numberType, numericStringType]));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/deepComparisons.types
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//// [tests/cases/compiler/deepComparisons.ts] ////

=== Performance Stats ===
Type Count: 2,500
Type Count: 1,000
Instantiation count: 2,500

=== deepComparisons.ts ===
Expand Down
76 changes: 76 additions & 0 deletions tests/baselines/reference/numericStringLiteralTypes2.symbols
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 tests/baselines/reference/numericStringLiteralTypes2.types
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;

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;