Skip to content

Type constraint not checked when a type parameter from a function is passed to a mapped typeΒ #49302

@JaredRohe

Description

@JaredRohe

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');

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptCursed?It's likely this is extremely difficult to fix without making something else much, much worseDomain: Mapped TypesThe issue relates to mapped types

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions