Narrowing survives closures after last assignment
TypeScript 5.4 preserves some narrowings inside callbacks after the last assignment. Check when you can remove the non-null assertion.
A function takes a prefix that may be null, fills in a default, and then maps over a list. TypeScript checked the null case two lines up, yet inside the .map callback it used to report 'prefix' is possibly 'null', so code like this collected ! assertions.
function tagAll(prefix: string | null, names: string[]) {
if (prefix === null) prefix = 'user'
return names.map((name) => `${prefix.toLowerCase()}-${name}`)
}With strict enabled, TypeScript 5.3 fails with error TS18047: 'prefix' is possibly 'null' on the callback line. On 5.4 and later it compiles as written.
What changed in 5.4
TypeScript 5.3 loses the narrowing in this callback because the function assigns to prefix. As of TypeScript 5.4, the checker can carry the narrowed type into a non-hoisted function created after the last assignment.
In tagAll, the assignment prefix = 'user' is the last one, and the arrow function is created after it. Nothing can set prefix back to null, so inside the callback it is a string.
Find the assertions you no longer need
Code written for older compilers says prefix!.toLowerCase(). The ! still compiles on 5.4, and tsc will not tell you it is redundant. The typescript-eslint rule no-unnecessary-type-assertion will, because it compares the type with and without the assertion.
$ npx eslint assert.ts
3:33 error This assertion is unnecessary since it does not change the type of the expression @typescript-eslint/no-unnecessary-type-assertionEnable type-aware linting and run the rule after upgrading. ESLint points to the redundant assertion in this example, and eslint --fix removes it.
Three shapes that still lose the narrowing
With strict enabled, each of these still reports TS18047 on TypeScript 5.4.
// 1. Assigned again after the callback is created
function later(prefix: string | null, names: string[]) {
if (prefix === null) prefix = 'user'
const tags = names.map((name) => `${prefix.toLowerCase()}-${name}`)
prefix = null
return tags
}
// 2. Assigned inside any nested function
function nested(prefix: string | null, names: string[]) {
if (prefix === null) prefix = 'user'
const reset = () => { prefix = 'user' }
reset()
return names.map((name) => `${prefix.toLowerCase()}-${name}`)
}
// 3. Used in a hoisted function declaration
function hoisted(prefix: string | null, names: string[]) {
if (prefix === null) prefix = 'user'
function tag(name: string) {
return `${prefix.toLowerCase()}-${name}`
}
return names.map(tag)
}In the first, the later prefix = null prevents narrowing even though this call to map runs synchronously. In the second, reset assigns a string, yet any assignment inside a nested function turns the rule off. In the third, a function declaration is hoisted, so the checker treats it as callable before the default is filled in.
The fix for all three is the one that always worked: copy the narrowed value into a const before the closure, as in const p = prefix ?? 'user', and use p inside it.