TypeScript Version: 3.6.3
Search Terms:
__spreadArrays, 3.6.3
Code
Make sure to run with --strictNullChecks off and compile for ES5!
function foo(b: number[]=null){
const a=[1,2,...b];
console.log(a);
}
foo();
Expected behavior:
No error should be produced by this code.
Actual behavior:
VM690:3 Uncaught TypeError: Cannot read property 'length' of null
at __spreadArrays (eval at <anonymous> (main-0.js:1018), <anonymous>:3:82)
at foo (eval at <anonymous> (main-0.js:1018), <anonymous>:11:13)
at eval (eval at <anonymous> (main-0.js:1018), <anonymous>:14:1)
at main-0.js:1018
Playground Link:
Error thrown in TS 3.6.3:
https://www.typescriptlang.org/play/?strictNullChecks=false&target=1#code/FAMwrgdgxgLglgewgAhAhAKARgLmRMAWywFMAnAbQF0BeAgG3oEoBvYZD5KJAZxmQCGNCgEYANACYxAOllYqAbnaduEHgnolp9BAHMMApkoC+wUOgxGgA
No error is thrown in TS 3.5.1:
https://www.typescriptlang.org/play/?strictNullChecks=false&target=1&ts=3.5.1#code/FAMwrgdgxgLglgewgAhAhAKARgLmRMAWywFMAnAbQF0BeAgG3oEoBvYZD5KJAZxmQCGNCgEYANACYxAOllYqAbnaduEHgnolp9BAHMMApkoC+wUOgxGgA
Related Issues:
none
The problem was probably introduced by this pull request: https://github.com/microsoft/TypeScript/pull/31166
Array-spreading null will result in an error when you target a version that supports this syntax, so I think it's fair to say that the transpiled version for other versions should fail as well.
IMO this is a bug in your code.
@MartinJohns in previous versions of Typescript this worked without any errors. This is quite a breaking change. So I'd be ok if this was reported as a breaking change and also provided some more adequate error message.
But since it just fails with an ugly message and nobody warned that we need to update our codebase, I see it as a bug.
Improved spec compliance is always considered a bug fix. I agree we should have put this in the blog post, but it's a bit late now.
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.
Most helpful comment
Array-spreading
nullwill result in an error when you target a version that supports this syntax, so I think it's fair to say that the transpiled version for other versions should fail as well.IMO this is a bug in your code.