Skip to content

Conditional types: cannot extend a string singleton type #30465

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
mgol opened this issue Mar 18, 2019 · 3 comments
Closed

Conditional types: cannot extend a string singleton type #30465

mgol opened this issue Mar 18, 2019 · 3 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@mgol
Copy link

mgol commented Mar 18, 2019

TypeScript Version: 3.4.0-dev.20190316

Search Terms:

  • extends string literal
  • conditional string singleton

Code

type Foo = {
    [T in string]: T extends 'a' ? number : string;
}
let foo: Foo = { a: 1, b: 'c' } as const;
foo['a' as 'a'] = 2 as const;
foo['a' as const] = 2 as const;

Expected behavior:

The code should not emit errors. TS should not expand the type of 'a' to string for the purpose of using this type in a conditional with the extends 'a' condition.

Actual behavior:

The attached code errors in all last 3 lines. It seems no matter how hard I try to tell the compiler I don't want to expand the type of 'a' into string it still does it.

Playground Link:

The above example uses TS 3.4 features so Playground won't work. But here's a version without as const that's parseable on the playground:
https://www.typescriptlang.org/play/#src=type%20Foo%20%3D%20%7B%0D%0A%20%20%20%20%5BT%20in%20string%5D%3A%20T%20extends%20'a'%20%3F%20number%20%3A%20string%3B%0D%0A%7D%0D%0Alet%20foo%3A%20Foo%20%3D%20%7B%20a%3A%201%2C%20b%3A%20'c'%20%7D%3B%0D%0Afoo%5B'a'%20as%20'a'%5D%20%3D%202%3B%0D%0A

Related Issues:

@dragomirtitian
Copy link
Contributor

dragomirtitian commented Mar 18, 2019

By the time you get those errors Foo will have already been resolved as { [x: string]: string }. The behavior of mapped types over string is to just make an index signature with string and instantiate the retrun type of the index with the key type parameter (T in this case) as string. So for example:

type Foo = {
    [T in string]: T[]
} /// just a fancy way of saying { [x: string]: string[]; }

I think Negated types (#29317) and the possibility to specify any valid key type in an index signature (#26797) will enable you to construct this type.

@rjamesnw
Copy link

Just to add, when you hover over Foo you actually see the type type Foo = { [x: string]: string; } pop up. ;)

@rjamesnw
Copy link

rjamesnw commented Mar 19, 2019

You can work around this by converting to the type another way:

type Foo<T> = {
    [K in keyof T]: K extends 'a' ? number : string;
}
function toFoo<T>(foo: T): Foo<T> { return <any>foo; }
let foo = toFoo({ a: 1, b: 'c' });

or

function toFoo<T>(foo: T): { [K in keyof T]: K extends 'a' ? number : string; } {
    return <any>foo;
}
let foo = toFoo({ a: 1, b: 'c' });

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Mar 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

5 participants