-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Open
Labels
BugA bug in TypeScriptA bug in TypeScriptCursed?It's likely this is extremely difficult to fix without making something else much, much worseIt's likely this is extremely difficult to fix without making something else much, much worseDomain: Mapped TypesThe issue relates to mapped typesThe issue relates to mapped types
Milestone
Description
Bug Report
π Search Terms
mapped types generic type constraint lost when passed from function
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about generics
β― Playground Link
Playground link with relevant code
π» Code
type SomeMappedType<T extends Record<string, any>> = {
[K in keyof T]: T[K]
}
function myFunc<T extends SomeMappedType<T>>(myArg: T){
}
const y: SomeMappedType<object> = {foo: 'bar'}; // okay. makes sense.
//@ts-expect-error
const x: SomeMappedType<string> = {foo: 'bar'}; // This error makes sense. `string` type parameter violates the Record<string, any> constraint
myFunc<string>('hey'); // Why is `string` not checked against the `Record<string, any>` constraint in this context ??π Actual behavior
No error when I pass in string as a type parameter to myFunc i.e myFunc<string>.
π Expected behavior
Typescript errors and warns me that string does not satisfy the constraint of Record<string, any> when I attempt to make the following function call: myFunc<string>('hey');
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptCursed?It's likely this is extremely difficult to fix without making something else much, much worseIt's likely this is extremely difficult to fix without making something else much, much worseDomain: Mapped TypesThe issue relates to mapped typesThe issue relates to mapped types