Skip to content

Template type fails to infer a matchΒ #48430

Closed
@wbt

Description

@wbt

Bug Report

πŸ”Ž Search Terms

Template literal type narrowing fails string 2345

πŸ•— Version & Regression Information

  • This changed between versions 4.0.5 and 4.1.5 because that is when the feature was introduced.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type EventName<T extends string> = `${T}Changed`; //From feature announcement: https://github.com/microsoft/TypeScript/pull/40336
const functionRequiresEventString = function(narrowedString: EventName<string>) {
	console.log('The following string is an event string per the original example: ' + narrowedString);
}
const nameEvent = function(whatChanged: string) {
	functionRequiresEventString('Functionality Changed');
	//Next line fails: Argument of type 'string' is not assignable to parameter of type '`${string}Changed`'.ts(2345)
	functionRequiresEventString(whatChanged + 'Changed');
}

πŸ™ Actual behavior

Error: Argument of type 'string' is not assignable to parameter of type '${string}Changed'.ts(2345) (but with backticks inside the quotes in the type).

πŸ™‚ Expected behavior

No error: The concatenated string ends in 'Changed' and should pass typechecking.

Related Issues

This is a minimal example using the first example of what should be supported, from the feature announcement, so I don't think reporting an issue in the announced functionality is a duplicate of this issue looking for gaps in what that didn't cover, and that thread is now being ignored anyway. This looks a lot like this issue reported 9/1/20 as a comment on the merged PR but is simpler than this issue around numbers. It’s also simpler than what this commenter sought around a fixed prefix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions