Skip to content

Regression with branded types in TypeScript 4.2 #42939

Closed
@TylorS

Description

@TylorS

Bug Report

When attempting to put together some conditional types to extract information about branded types, TS 4.2 is no longer able to correctly infer your intent.

type Branded<T, A> = A & { __brand: T }
type BrandedValue<A> = A extends Branded<unknown, infer R> ? R : never 

// Expected to be 'string' but is 'string & { __brand: T }' in 4.2+
type T1 = BrandedValue< Branded< 'GUID', string > >

🔎 Search Terms

  • Branded Types
  • Conditional Types
  • TypeScript 4.2

🕗 Version & Regression Information

  • This changed between versions 4.1.5 and 4.2 beta

⏯ Playground Link

Working example in 4.1.5

https://www.typescriptlang.org/play?#code/C4TwDgpgBAQgTgQwHYBMIoDwBUA0UCCAfFALwFQBkUA3lAPp0BGiqAXFFlAL4BQPokWCxQB5AGYYipKAG18AXSgQAHsAioAzrPjI0mAJZIxEOFABKeAK5IA1kgD2AdySFFAfnNR2SCADcTUPzg0Dqo6ABqCAA2lhCSxGT4SqrqKFqhehjWdk5IeIbGpmbEHmZeUD7+pnwC0ADilvoo0hnoGABEDU3teBrAcIYA5oRBgl0oGS3C4hjjI7VQ45Ex0GStKMuxs40oI0A

Not working in 4.2
https://www.typescriptlang.org/play?ts=4.2.0-beta#code/C4TwDgpgBAQgTgQwHYBMIoDwBUA0UCCAfFALwFQBkUA3lAPp0BGiqAXFFlAL4BQPokWCxQB5AGYYipKAG18AXSgQAHsAioAzrPjI0mAJZIxEOFABKeAK5IA1kgD2AdySFFAfnNR2SCADcTUPzg0Dqo6ABqCAA2lhCSxGT4SqrqKFqhehjWdk5IeIbGpmbEHmZeUD7+pnwC0ADilvoo0hnoGABEDU3teBrAcIYA5oRBgl0oGS3C4hjjI7VQ45Ex0GStKMuxs40oI0A

Not working in Nightly
https://www.typescriptlang.org/play?ts=4.3.0-dev.20210224#code/C4TwDgpgBAQgTgQwHYBMIoDwBUA0UCCAfFALwFQBkUA3lAPp0BGiqAXFFlAL4BQPokWCxQB5AGYYipKAG18AXSgQAHsAioAzrPjI0mAJZIxEOFABKeAK5IA1kgD2AdySFFAfnNR2SCADcTUPzg0Dqo6ABqCAA2lhCSxGT4SqrqKFqhehjWdk5IeIbGpmbEHmZeUD7+pnwC0ADilvoo0hnoGABEDU3teBrAcIYA5oRBgl0oGS3C4hjjI7VQ45Ex0GStKMuxs40oI0A

🙁 Actual behavior

The brand is not removed from the return type

🙂 Expected behavior

The brand should be removed from the return type

Metadata

Metadata

Labels

Breaking ChangeWould introduce errors in existing codeFix AvailableA PR has been opened for this issueWorking as IntendedThe behavior described is the intended behavior; this is not a bug

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions