Start off by running the default set of strictNullCheck
fixes.
This will add !
s as necessary to any locations that use nullable types unsafely.
For example, this configuration will add !
s only to *.test.ts
test files, such as with VS Code's strict tests push:
{
"fixes": {
"strictNonNullAssertions": true
},
"include": ["./src/**/*.test.ts"],
"types": {
"strictNullChecks": true
}
}
Alternately, you can enable the incompleteTypes
fix, which will prefer adding | null
and | undefined
to types as properties, parameters, and so on.
This configuration will add those with:
filters
to ignore class and test disposal methods, as they might assignnull
orundefined
unnecessarilytypes.matching
to only addnull
andundefined
as typestypes.onlyPrimitives
to skip computing advanced types as a performance improvement
{
"filters": [
"CallExpression[expression.text=suiteTeardown]",
"CallExpression[expression.text=teardown]",
"MethodDeclaration[name.text=dispose]"
],
"fixes": {
"incompleteTypes": true
},
"types": {
"matching": ["^(null|undefined)$"],
"onlyPrimitives": true,
"strictNullChecks": true
}
}
Many more places will need !
assertions after parameters and properties are given the new nullable types.
Consider running the earlier !
fixes on your test files first, then running this on your source files.
To clean up code after this set is applied, consider using the
no-non-null-assertion
ESLint rule and its auto-fixer over time.