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

Broken null checks for properties accessed via the brackets notation. #15582

Closed
kujon opened this issue May 4, 2017 · 2 comments
Closed

Broken null checks for properties accessed via the brackets notation. #15582

kujon opened this issue May 4, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@kujon
Copy link
Contributor

kujon commented May 4, 2017

TypeScript Version: 2.3.2

Code

type MyType = {
    value?: string;
};

const obj: MyType = {
    value: '2017-05-04T13:18:21.203Z'
};

// -------------------------------

/**
 * Works!
 */
const a = obj.value ? new Date(obj.value) : null;

// -------------------------------

const VALUE = 'value';

/**
 * Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
 * Type 'undefined' is not assignable to type 'string'.
 */
const b = obj[VALUE] ? new Date(obj[VALUE]) : null;

// -------------------------------

/**
 * Works with a helper variable.
 */
const temp = obj[VALUE];

const c = temp ? new Date(temp) : null;

Expected behavior:
All 3 examples compile.

Actual behavior:

 Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
 Type 'undefined' is not assignable to type 'string'.
@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label May 4, 2017
@RyanCavanaugh
Copy link
Member

See #12636

@mhegazy
Copy link
Contributor

mhegazy commented Mar 2, 2018

this is actually a duplicate of #10530

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants