Skip to content

TypeScript conditional types constrain no longer working after v3.5.1 #40029

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
weichensw opened this issue Aug 13, 2020 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@weichensw
Copy link

weichensw commented Aug 13, 2020

TypeScript Version: versions after 3.5.1, tested up to 4.1.0-dev.20200807

Search Terms: conditional constrain never

Code

type StringConstrain<T> = { [K in keyof T]: T[K] extends string ? T[K] : never }
type WantString<T extends string> = number
type Test<T extends StringConstrain<T>> = { [K in keyof T]: WantString<T[K]> }

Expected behavior:

The constrain in line 1 should function correctly and demands all keys of T to extend string in line 3, as such WantString<T[K]> should be happy.

Actual behavior:

In any version greater and including 3.5.1 line 3 is not happy

Type 'T[K]' does not satisfy the constraint 'string'.
  Type 'T[keyof T]' is not assignable to type 'string'.
    Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'string'.
      Type 'T[string]' is not assignable to type 'string'.(2344)

I don't understand the new behavior and if this is not a bug, what's the intended way of achieving the same type constrain after 3.5.1?

Playground Link:

Playground link showing works in v3.3.3

Playground link showing not working in 3.9.2

Playground link showing not working in 4.1.0-dev.20200807

Related Issues:

Same question on stackoverflow from last month: https://stackoverflow.com/questions/62563003/typescript-conditional-types-constrain-no-longer-working-after-v3-5-1

@jack-williams
Copy link
Collaborator

Duplicate of #32550. It was probably working in <3.5.1 for the wrong reasons.

Does this work for you? type Test<T extends Record<keyof T, string>> = { [K in keyof T]: WantString<T[K]> }

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 20, 2020
@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

4 participants