Typescript - Strict flags
Very strict flags
Overview of flags in article by Axel Rauschmayer
--strictNullChecks
Definition:
- Objects can’t take on the values of
nullorundefinedif not explicitly specified. - If this flag is not set, every JS object can take on
nullorundefined. So the typesnullandundefinedare widened toany. - see also this article
e.g. leads to having a [] without contextual type to get inferred to never[]. This can be made explicit by casting to type any[], e.g. [] as any[].
Explanation:
The issue is what is the element type of
[]if there is no contextual type?undefinedseems reasonable since there is not a better source of information available. This works in non strict null checks mode becauseundefinedis in the domain of all types, and gets widened toany. InstictNullChecksit does not, sinceundefinedis not any other type exceptundefined; and in that sense,neveris a better name for this condition. So it is notneverthat is the issue.
--noImplicitAny
Definition from Axel Rauschmayer:
If TypeScript can’t infer a type, you must specify it. This mainly applies to parameters of functions and methods: With this settings, you must annotate them.
Throws an error when the type of something is implicitly widened to any, e.g. when empty array literals are widened from never[] to any[].
noImplicitAny is often used together with strictNullChecks. This discussion shows how the implicit widening of never[] to any[] is disabled with strictNullChecks already to prevent noImplicitAny from throwing an error then.
Discuss on Twitter ● Improve this article: Edit on GitHub