Skip to content

Type checker incorrectly refuses object spread of null or undefined #12460

Closed

Description

TypeScript Version: 2.2.0-dev.20161122

With --strictNullChecks.

Code

function spread(arg?: {}): {} {
  return {...arg}
}
spread()

Expected behavior:

spread() returns empty object {}.

Actual behavior:

Spread types may only be created from object types.


The spec specifically allows null and undefined spread arguments by having an early check before the ToObject abstract operation (3.1.2).

Activity

  1. added this to the milestone on Nov 30, 2016
  2. mhegazy commented on Nov 30, 2016

    @mhegazy
    Contributor

    @Kovensky please give tonight's typescript@next a try and let us know if you are still seeing issues.

  3. Jessidhia commented on Nov 30, 2016

    @Jessidhia
    Author

    Mohamed Hegazy (@mhegazy) is there a way to know which commit was used to build a given nightly?

  4. mhegazy commented on Nov 30, 2016

    @mhegazy
    Contributor

    The nightly snaps at 11:00 pm PST, anything merged in master before that should be there.

  5. mjomble commented on May 26, 2018

    @mjomble

    I found some cases where this is still an issue:

    // Logs three empty objects without errors in JavaScript.
    // Shows three "Spread types may only be created from object types." errors in TypeScript.
    // Tested on TypeScript Playground on May 26, 2018.
    
    const spread = <T extends object>(arg: T | null) => ({ ...arg })
    console.log(spread(null), { ...null }, { ...undefined })

    Playground link

  6. locked and limited conversation to collaborators on Jul 31, 2018
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

    BugA bug in TypeScriptFixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions