Skip to content

Using && with spread operator causes "overridden" error if one of the types is "any" #39113

Closed
@mastrzyz

Description

@mastrzyz

We are having problems with TypeScript 3.9 upgrade for our repositories and the spread operator and "&&" with any types. TypeScript compiler says the value will be always overridden even though an any type isn't 100% always null/undefined.

TypeScript Version: 3.9.5
Search Terms:

  • specified more than once, so this usage will be overwritten.
  • any type and spread operator

Code

Doesn't work

let isTreeHeader : any = null!;
function foo(){
    return {
        display: "block",
        ...(isTreeHeader && {
          display: "flex",
        })
    }
}
foo()

Works

let isTreeHeader : number = null!;
function foo(){
    return {
        display: "block",
        ...(isTreeHeader && {
          display: "flex",
        })
    }
}
foo()

Expected behavior:

any type can be undefined or can have a non nullable value so we shouldn't get an error with the spread operator

Actual behavior:

'display' is specified more than once, so this usage will be overwritten.ts(2783)

Playground Link:
https://www.typescriptlang.org/play/?ssl=1&ssc=1&pln=2&pc=22#code/DYUwLgBAlgzgKgJxCAEiAhgExAiAuCdAOwE8IBeCIgV2GAEIBuAKADNqiBjMKAeyIitevABQBKAN7MISMNQQCpETLAAOwdCQIAiAEbBenANbaANNIB0VkbETI0WHBABkziEpUx1mna1AAPM2kAXzEQ5mC2YXEgA

Related Issues:

Metadata

Metadata

Assignees

Labels

Breaking ChangeWould introduce errors in existing codeBugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions