Skip to content

Unexpected behavior of conditional types on generics #33296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nscarcella opened this issue Sep 7, 2019 · 5 comments
Closed

Unexpected behavior of conditional types on generics #33296

nscarcella opened this issue Sep 7, 2019 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@nscarcella
Copy link

I stumbled upon a rather odd behavior while trying to combine conditional types with generics. It looks like, even when the produced types are structurally different, it's still possible to assign a values between them.

class T1 { t1_tag = 't1' }
class T2 extends T1 { t2_tag = 't2' }

type X<T> = {
  x: T extends T2 ? 'yes' : 'no'
}

let a1: X<T1> = { x: 'no' } // {x: 'yes'} is not valid
let a2: X<T2> = { x: 'yes' } // {x: 'no'} is not valid

// These assignations should not type, but they do
a1 = a2
a2 = a1

At first I thought this might be caused by generics being intentionally ignored but that seemed weird, since the types end up being structurally different and checks seem to work for literals, so I though I would report it, just in case.

TypeScript Version: 3.6.2

Search Terms: generics, conditional type, type inheritance

Expected behavior: exclusive structural conditional types derived from generic should not be assignable to each other.

Actual behavior: assignation seems to pass type check even when types are structurally incompatible.

Playground Link: here

@AnyhowStep
Copy link
Contributor

AnyhowStep commented Sep 7, 2019

I'm on mobile,

Playground

@weswigham

This variation gives errors. This looks similar to things I've seen before. Is this one of those variance markers not being propagated properly?

@jack-williams
Copy link
Collaborator

jack-williams commented Sep 7, 2019

Duplicate of #31295

Please ignore me failing to use github correctly

@jack-williams jack-williams marked this as a duplicate and then as not a duplicate of #31295 Sep 7, 2019
@fatcerberus
Copy link

Duplicate of #31295

But... there's no any involved here... I don't think...

@jack-williams
Copy link
Collaborator

The any is independent of the root issue. Check types in conditionals cause type parameters to be incorrectly measured as bivariant.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Sep 12, 2019
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

6 participants