Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

__spreadArrays throws 'Cannot read property 'length' of null' when --strictNullChecks is off #33549

Closed
KirillMetrik opened this issue Sep 23, 2019 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@KirillMetrik
Copy link

KirillMetrik commented Sep 23, 2019

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: #31166

@MartinJohns
Copy link
Contributor

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.

@KirillMetrik
Copy link
Author

@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.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Sep 23, 2019
@RyanCavanaugh
Copy link
Member

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.

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants