Skip to content
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

Type inference in conditional types doesn't respect constraints #21937

Closed
falsandtru opened this issue Feb 14, 2018 · 4 comments
Closed

Type inference in conditional types doesn't respect constraints #21937

falsandtru opened this issue Feb 14, 2018 · 4 comments
Assignees
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@falsandtru
Copy link
Contributor

cc @ahejlsberg

TypeScript Version: master

Search Terms:

Code

type A<T extends string[]> = T extends Array<infer U> ? Record<U, {}> : never;

Expected behavior:

pass

Actual behavior:

$ node built/local/tsc.js --noEmit index.ts
index.ts(1,64): error TS2344: Type 'U' does not satisfy the constraint 'string'.

Playground Link:

Related Issues:

@jcalz
Copy link
Contributor

jcalz commented Feb 14, 2018

Related to #21729 ?

@DanielRosenwasser DanielRosenwasser added the Bug A bug in TypeScript label Feb 14, 2018
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 2.8 milestone Feb 14, 2018
@DanielRosenwasser
Copy link
Member

I'll let Anders decide if this is a dupe or not.

@falsandtru
Copy link
Contributor Author

I think type parameter constraints and type narrowing by conditional types are different.

@ahejlsberg
Copy link
Member

I'm going to call this working as intended. While it might be possible to transfer constraints when the checked type is a simple type variable, it's not clear how we could do it in the general case. Also, it's quite easy to manually add the constraint:

type A<T extends string[]> =
    T extends Array<infer U> ? U extends string ? Record<U, {}> : never : never;

Or, now that we have #22323:

type StringArray<T extends string> = T[];
type A<T extends string[]> = T extends StringArray<infer U> ? Record<U, {}> : never;

@ahejlsberg ahejlsberg added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Bug A bug in TypeScript labels Mar 5, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants