TypeScript Version: 2.4.0
Code
Consider the following snippet:
Typescript infers EMPTY_ARRAY to be any[] and doesn't complain even with noImplicitAny turned on. Things get weirder, when we start using this variable:
const EMPTY_ARRAY = []; // Variable 'EMPTY_ARRAY' implicitly has type 'any[]' in some locations where its type cannot be determined.
const foo = () => EMPTY_ARRAY; // Variable 'EMPTY_ARRAY' implicitly has an 'any[]' type.
We get no implicit any error in 2 places. A questionable attempt at fixing this might be to type EMPTY_ARRAY as never[] or ReadonlyArray<never> which results in no errors:
const EMPTY_ARRAY: ReadonlyArray<never> = [];
const foo = () => EMPTY_ARRAY;
Questionability of this 'fix' aside, one would expect that turning off strictNullChecks would result in the code continuing to compile, but it's not the case:
// --strictNullChecks false
const EMPTY_ARRAY: ReadonlyArray<never> = [];
/** Type 'undefined[]' is not assignable to type 'ReadonlyArray<never>'.
Types of property 'concat' are incompatible.
Type '{ (...items: undefined[][]): undefined[]; (...items: undefined[][]): undefined[]; }' is not assignable to type '{ <U extends ReadonlyArray<never>>(...items: U[]): never[]; (...items: never[][]): never[]; (...i...'.
Type 'undefined[]' is not assignable to type 'never[]'.
Type 'undefined' is not assignable to type 'never'. */
const foo = () => EMPTY_ARRAY;
Expected behavior:
Not sure how this should be fixed tbh. To me it seems like never[] is not a type that should be allowed in either mode, since mutating the array will certainly break this. ReadonlyArray<never> looks like a valid option. On top of this, I'd expect that making the compiler less strict will keep the strict code compiling.
Actual behavior:
Described above.
TypeScript Version: 2.4.0
Code
Consider the following snippet:
Typescript infers
EMPTY_ARRAYto beany[]and doesn't complain even withnoImplicitAnyturned on. Things get weirder, when we start using this variable:We get no implicit any error in 2 places. A questionable attempt at fixing this might be to type
EMPTY_ARRAYasnever[]orReadonlyArray<never>which results in no errors:Questionability of this 'fix' aside, one would expect that turning off
strictNullCheckswould result in the code continuing to compile, but it's not the case:Expected behavior:
Not sure how this should be fixed tbh. To me it seems like
never[]is not a type that should be allowed in either mode, since mutating the array will certainly break this.ReadonlyArray<never>looks like a valid option. On top of this, I'd expect that making the compiler less strict will keep the strict code compiling.Actual behavior:
Described above.