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

Create a new type after deleting a property from object #42069

Open
bezreyhan opened this issue Dec 21, 2020 · 0 comments
Open

Create a new type after deleting a property from object #42069

bezreyhan opened this issue Dec 21, 2020 · 0 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@bezreyhan
Copy link

bezreyhan commented Dec 21, 2020

TypeScript Version: v4.13

Search Terms: delete property from object new type

Code

interface ApiData {
    foo?: string | null;
}

interface Props {
    foo?: string;
}

function test(props: Props) {
    console.log(props.foo);
} 

const bar: ApiData = {
    foo: null
}

if (bar.foo === null) {
    delete bar.foo;
}

test(bar);

Expected behavior:
No type error should occur when calling test with the argument bar since we have deleted foo from bar when foo is null.

Actual behavior:
The following error is reported by TS: Argument of type 'ApiData' is not assignable to parameter of type 'Props'. Types of property 'foo' are incompatible. Type 'string | null | undefined' is not assignable to type 'string | undefined'. Type 'null' is not assignable to type 'string | undefined'.(2345)

Playground Link: https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgIIAdgBE5jsgbwChlTkYB7CgfgC5kBnMKUAc2QB9kQBXAGz4BuIgF8iRUJFiIUABSgV0DQiTKUa9JixCthYojB4gEYYBRDJITABToFS+vMUMAlCrLIE5hhT4QAdHwUrLb2DP7qLnrI4l4gTMgARnBQ9BjYuPgAvO5qVPS8AqLiwDDI1slQEVTIWXXc-HxuxB4AJhB+kEkp1RR64lZgFSlRQA

Related Issues: #13783

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Dec 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants