microsoft / microsoft/TypeScript

Create a new type after deleting a property from object

Open
#42,069 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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: https://github.com/microsoft/TypeScript/issues/13783

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the TypeScript v4.13 example in the linked Playground and compare the reported assignability error with the expected behavior after deleting bar.foo. Read related issue #13783 for prior context; done means the example no longer reports the error while preserving correct handling of nullable properties.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.