-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Open
Labels
Possible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone
Description
π Search Terms
indexed access substitution type array constraint
π Version & Regression Information
- This is the behavior in every version I tried
β― Playground Link
π» Code
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 extends SchemaObject
? {
[K in keyof S]: InferValuesFromSchema<S[K]>;
}
: never;
π Actual behavior
an error is reported:
Type 'S[K]' does not satisfy the constraint 'Schema'.
Type 'S[keyof S]' is not assignable to type 'Schema'.
Type 'S[string] | S[number] | S[symbol]' is not assignable to type 'Schema'.
Type 'S[string]' is not assignable to type 'Schema'.
Type 'S[string]' is not assignable to type 'SchemaObject'.
Type 'S[keyof S]' is not assignable to type 'SchemaObject'.
Type 'S[K]' is not assignable to type 'SchemaObject'.
Type 'S[keyof S]' is not assignable to type 'SchemaObject'.
Type 'S[string] | S[number] | S[symbol]' is not assignable to type 'SchemaObject'.
Type 'S[string]' is not assignable to type 'SchemaObject'.(2344)
π Expected behavior
There should be no error, S[K]
should satisfy Schema
constraint (at least since #48837 )
Additional information about the issue
A variant with an additional type alias for that array branch works OK: TS playground.
kettanaito and ryan-0324whzx5byb
Metadata
Metadata
Assignees
Labels
Possible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases