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

delete operator and strict null checks #15629

Closed
jmlopez-rod opened this issue May 6, 2017 · 2 comments
Closed

delete operator and strict null checks #15629

jmlopez-rod opened this issue May 6, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@jmlopez-rod
Copy link

TypeScript Version: 2.3 (the one in the playground)

Code

interface IName {
    name?: string;
}

class A {
    prop: IName;

    constructor(param: IName) {
        this.prop = param;
    }

    printProp() {
        if (this.prop) {
            // this.prop = undefined;  // TS will catch this error
            delete this.prop;  // Oh no, now we'll have a runtime error
            console.log(this.prop.name);
        }
    }
}

const a = new A({ name: 'a' });
a.printProp();

Or click here to see it in the playground.

Expected behavior:

Should warn us about using the delete operator since we told typescript that prop is an IName.

Actual behavior:

Currently as the code stands typescript is not able to warn us of the oncoming failure. I have left the line this.prop = undefined and typescript will complain about it. Using the delete operator will do the same as setting as undefined but there is no warning.

Make sure to use the strictNullChecks option when running the code.

@mhegazy
Copy link
Contributor

mhegazy commented May 8, 2017

looks like a duplicate of #13783

@mhegazy mhegazy added the Duplicate An existing issue was already created label May 8, 2017
@jmlopez-rod
Copy link
Author

That seems to be the case.

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 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

2 participants