Typescript: Index signature incorrectly includes `undefined` when computed property name and spread are both present

Created on 26 Jun 2020  ·  1Comment  ·  Source: microsoft/TypeScript

TypeScript Version: 4.0.0-beta


Search Terms: index signature spread undefined computed

Code

declare const m: { [k: string]: string };
const x: { [k: string]: string } = { ...m, ["a" + "b"]: "" };

Expected behavior: No error (3.9.3)

Actual behavior: Error

a.ts:2:7 - error TS2322: Type '{ [x: string]: string | undefined; }' is not assignable to type '{ [k: string]: string; }'.
  Index signatures are incompatible.
    Type 'string | undefined' is not assignable to type 'string'.
      Type 'undefined' is not assignable to type 'string'.

2 const x: { [k: string]: string } = { ...m, ["a" + "b"]: "" };
        ~

Playground Link: linky

Related Issues: no

Bug Fix Available

Most helpful comment

Caught this in our CI earlier, was just about to enter an Issue, thanks!

Links to 3.9.2 and 4.0.0-beta.

>All comments

Caught this in our CI earlier, was just about to enter an Issue, thanks!

Links to 3.9.2 and 4.0.0-beta.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fwanicka picture fwanicka  ·  3Comments

uber5001 picture uber5001  ·  3Comments

weswigham picture weswigham  ·  3Comments

jbondc picture jbondc  ·  3Comments

MartynasZilinskas picture MartynasZilinskas  ·  3Comments