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

Bug: strictNullChecks + object spread + computed key #37103

Open
bogdan opened this issue Feb 28, 2020 · 1 comment
Open

Bug: strictNullChecks + object spread + computed key #37103

bogdan opened this issue Feb 28, 2020 · 1 comment
Labels
Bug A bug in TypeScript
Milestone

Comments

@bogdan
Copy link

bogdan commented Feb 28, 2020

TypeScript Version: 3.8.2

Search Terms:

  • strictNullChecks
  • object spread
  • computed key

Code

const b = { a: 'hello' };
const key: string = 'hello';
const a: Record<string, string> = { ...b, [key]: undefined };

Expected behavior:

Type check fails

Actual behavior:

No Typecheck error.

It is interesting that if we remove explicit string type for key the code is not compilable as expected.

Playground Link:
https://www.typescriptlang.org/play/?ssl=1&ssc=1&pln=4&pc=1#code/MYewdgzgLgBARjAvDA3jAhgLhgcgBYCmANkSDjAL4DcAsAFCiSwDWBAnttAE4CWYA5klyESZWg3DQM2AEoFQXACYAebn34AaGGoEA+IWgB0xuFoDarNgF1sAVzCKCAMz4FFlcUA

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Mar 10, 2020
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 10, 2020
@arnodb
Copy link

arnodb commented Apr 28, 2020

I may have found a related or similar or "having the same cause" or "could potentially have the same fix" bug:

interface Foo {
    a: number,
}

interface Bar {
    a?: number,
}

let foo: Foo = {
    a: 42,
};

const bar: Bar = {
    a: undefined,
}

foo = {
    ...foo,
    ...bar,
};

TSC (3.8.3 and earlier) in strict mode accepts the undefined value whereas it leads to foo not being of type Foo. Nodejs copies all entries found in bar, including the ones with an undefined value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants