declare let v: string | number
if (typeof v === "string") {
v.substring(1)
}
// and
let obj: {
key: "literal"
}
with tslint.json configuration:
{
"rules": {
"quotemark": [true, "backtick"]
}
}
Quotes are flagged (and changed to backticks with --fix)
The quotes remain as they are, because this version of TS does not narrow the type of v when using typeof with a backtick string, and does not compiler at all when using a backtick string as a literal type
Quoting @ericbf from #4642:
Typescript below the listed version fails compilation (or otherwise misbehaves) when backticks are used in some places where it succeeds in future versions. For instance,
typeof str === `string`does not narrow the type ofstrin TS versions below 2.7.1, causing compilation errors if you try to use it as a string later. In the other cases, older TS throws errors where NoSubstitutionLiterals are used instead of single/double quoted strings, but works fine in above 2.7.1; rather than cripple the rule to support order versions, I split it up.
See the
typeofissue here. Its milestone was 2.7 (hence the split in this PR).
And here's the issue for backticks strings as types. This one was 2.6, but rather than doing multiple splits, I just did one at 2.7.
Weird – GitHub originally didn't save the issue info 🤷🏻♂️
👻 spooky! CircleCI is also being flaky today...