Skip to content

Type argument with a subset of the parameter constraint's optional keys incorrectly reported as unassignable to constraintΒ #63725

Description

@aweebit

πŸ”Ž Search Terms

"does not satisfy the constraint" "keyof" "optional"

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about "constraint"

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAsiAq4IB54BooFUoQB7AgDsATAZygG8oBtAaSgEtCoBrCEAewDMp4BdAPwAuKAFdCLQhwDuzAL4A+KAF4sAbgCwAKG0BjADYBDUuTgBhIydRKK2qFABuh-aIgBCEXESRUGKnUZmNk4eeCgAMihSYAAnJgBzQRFxSRl5BU0tOW0gA

πŸ’» Code

type MyType<T, U extends { [K in keyof T]?: unknown }> = U;

class MyClass<T> {
  value!: MyType<T, { [K in keyof T & string]?: unknown }>;
}

πŸ™ Actual behavior

The following error is reported:

Type '{ [K in keyof T & string]?: unknown; }' does not satisfy the constraint '{ [K in keyof T]?: unknown; }'.
  Type 'keyof T' is not assignable to type 'keyof T & string'.
    Type 'string | number | symbol' is not assignable to type 'keyof T & string'.
      Type 'string' is not assignable to type 'keyof T & string'.
        Type 'string' is not assignable to type 'keyof T'.
          Type 'keyof T' is not assignable to type 'string'.
            Type 'string | number | symbol' is not assignable to type 'string'.
              Type 'number' is not assignable to type 'string'.

πŸ™‚ Expected behavior

No error should be reported because due to the keys being optional, { [K in keyof T & string]?: unknown } is a subtype of { [K in keyof T]?: unknown } for all T.

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions