Skip to content

Consistent inferences when inferring to template literal type #40518

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

Merged
merged 4 commits into from
Sep 13, 2020

Conversation

ahejlsberg
Copy link
Member

This PR makes a minor adjustment to template literal inference to make results more consistent:

type Foo<T> = T extends `*${infer S}*` ? S : never;

type T1 = Foo<any>;      // never (previously string)
type T2 = Foo<string>;   // never (previously string)
type T3 = Foo<'abc'>;    // never
type T4 = Foo<'*abc*'>;  // 'abc'

Inference now consistently infers never for all placeholder type variables when the source string isn't a literal type that matches the pattern.

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Sep 12, 2020
# Conflicts:
#	tests/baselines/reference/templateLiteralTypes1.errors.txt
#	tests/baselines/reference/templateLiteralTypes1.symbols
@DanielRosenwasser
Copy link
Member

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 12, 2020

Heya @DanielRosenwasser, I've started to run the tarball bundle task on this PR at a3a2db2. You can monitor the build here.

@DanielRosenwasser
Copy link
Member

Just checking, it doesn't matter whether the template has any literal content, right? For example, could I write the following and have it work (apart from any and never)?

type IsStringLiteral<T> = T extends `${infer U}` ? U extends never ? false : true : false;

@DanielRosenwasser
Copy link
Member

I think the behavior here is surprising, but is probably desirable.

@ahejlsberg ahejlsberg merged commit 57c8938 into master Sep 13, 2020
@ahejlsberg ahejlsberg deleted the consistentTemplateLiteralInference branch September 13, 2020 02:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants