Skip to content

A Type Error occurs with using a generic that extends a branded intrinsic typeΒ #55040

Description

Bug Report

πŸ”Ž Search Terms

  • generics
  • branded
  • intrinsic type

πŸ•— 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

/////////////////////////////////////////////////////////////////////////////////////////////// TypestypeBrandedNumber=number&{__branded: true}typeOperationIO<Textendsnumber>=TextendsBrandedNumber
? T
: number;/////////////////////////////////////////////////////////////////////////////////////////////// Functionfunctionsub<Textendsnumber>(a: OperationIO<T>,): (b: OperationIO<T>)=>OperationIO<T>{return(b)=>(b-a)asany;}/////////////////////////////////////////////////////////////////////////////////////////////// Tests// Make sure non-branded numbers work.consttestNumbers=sub(2)(3);// Make sure branded numbers work.functiontestWithoutGenerics(a: BrandedNumber,b: BrandedNumber){returnsub(a)(b);}// Subtypes of branded numbers should work too.functiontestWithGenerics<TextendsBrandedNumber>(a: T,b: T){returnsub(a)(b);// <-- Why is this not allowed?}// Has correct typing dispite above error.consttestResultWithGenerics=testWithGenerics(2asBrandedNumber&{__foo: 1},3asBrandedNumber&{__foo: 1});

πŸ™ Actual behavior

TypeScript reports a type error on line 35.

πŸ™‚ Expected behavior

No error should be reported because as far I can tell, this line should be valid. Additionally, TypeScript can successfully deduce the type on line 39.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions