microsoft / microsoft/TypeScript
Create a new type after deleting a property from object
Nobody has claimed this yet.
- 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)
Related Issues: https://github.com/microsoft/TypeScript/issues/13783
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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