Skip to content

Generic constraint fails with Partial<Record<keyof T, any>> but works with { [K in keyof T]?: any }Β #62717

@russelldavis

Description

@russelldavis

πŸ”Ž Search Terms

partial record mapped type generic constraint

πŸ•— Version & Regression Information

  • This changed between versions 3.9.7 and 4.0.5

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.9.3#code/C4TwDgpgBA6g9gJwNYGcA8AVKEAewIB2AJilHAEYBWEAxsADRQCq2ehJUA3lANoDSUAJYEoSCCDgAzKBgC6AfgBcUAIYEQUAL4A+KAF5mAbgBQoSFABiKwQBt0WXPmKkK1OoxaP2pAAoqEwIIqNmgASrSIRGhiEtIYjGog2sn6Rsam4NAAggBCqZwqyijACMIA5ozkisWlBGWaGeZ5BpxVUDXlDcYA9N0yABYQKNDkcMD9UADuiEiqxNgAbsEArir4UMBwG4PtKgC20GYQjdAYQ8DwyCgAjKmXqGi5jDnaJzLnVrY3qZ92jznPV49PoAOS2EAQCEQKDeZ2K9xQACZMKwnBxcroDAjMIDgVAAKKQxDKDCZKAAchy5KgRDgQygBDGu0CKEkGnG0BocAINWsBGAFL8ASCIXCXIQURiUhkCXUyXJADpYR9rHZkQ42M4oBifqr7LigA

πŸ’» Code

type Works<T extends object, U extends { [K in keyof T]?: any }> = U;
type Fails<T extends object, U extends Partial<Record<keyof T, any>>> = U;

type AB = {a: string, b:string}
type B = {b: string}

// These both work and evaluate to the same type
type TestWorks1 = Works<AB, B>
type TestFails1 = Fails<AB, B>

// No errors
type TestWorks2<T extends AB> = Works<T, B>
// Error: Type 'B' does not satisfy the constraint 'Partial<Record<keyof T, any>>'.
type TestFails2<T extends AB> = Fails<T, B>

πŸ™ Actual behavior

  • Works and Fails behave differently
  • TestFails2 results in an error

πŸ™‚ Expected behavior

  • Works and Fails behave the same
  • TestFails2 does not result in an error

Additional information about the issue

@easrng narrowed down the divergence in behavior between the two types to this line in checker.ts (screenshot because the file is too big for github to display):

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions