Skip to content

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

Description

@KirillMetrik

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

Activity

  1. MartinJohns commented on Sep 23, 2019

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

  2. KirillMetrik commented on Sep 23, 2019

    @KirillMetrik
    Author

    Martin Johns (@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.

  3. RyanCavanaugh commented on Sep 23, 2019

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

  4. typescript-bot commented on Sep 25, 2019

    @typescript-bot
    Contributor

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

  5. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions