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

Multiple issues with template literal type narrowing and relations #46125

Closed
ahejlsberg opened this issue Sep 28, 2021 · 1 comment Β· Fixed by #46137
Closed

Multiple issues with template literal type narrowing and relations #46125

ahejlsberg opened this issue Sep 28, 2021 · 1 comment Β· Fixed by #46137
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@ahejlsberg
Copy link
Member

ahejlsberg commented Sep 28, 2021

πŸ•— Version & Regression Information

Every version.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

function f1(x: `foo-${string}`, y: `${string}-bar`) {
    if (x === y) {  // Wrongly reports error that types don't overlap (they do, e.g. 'foo-xxx-bar')
    }
}

function f2(x: string, y: `foo-${string}` | 'bar') {
    if (x === y) {
        x;  // Narrowed to 'bar', should be `foo-${string}` | 'bar'
    }
}

function f3(x: string, y: `foo-${string}`) {
    if (x === 'foo-test') {
        x;  // Narrowed to 'foo-test'
    }
    if (y === 'foo-test') {
        y;  // Not narrowed but should be
    }
}

πŸ™ Actual behavior

See above.

πŸ™‚ Expected behavior

See above.

@ahejlsberg ahejlsberg added the Bug A bug in TypeScript label Sep 28, 2021
@ahejlsberg ahejlsberg self-assigned this Sep 28, 2021
@ahejlsberg ahejlsberg added this to the TypeScript 4.5.1 milestone Sep 28, 2021
@craigphicks
Copy link

craigphicks commented Sep 29, 2021

That was very kind of you to attempt to classify fixed prefixes - and fixed mid?/post-fixes too. I would have thought that classifying fixed prefixes alone would be sufficient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants