简体   繁体   中英

What is the logic behind the TypeScript error "The operand of a 'delete' operator must be optional"?

This is the new error that is coming in typescript code.

I am not able to realize the logic behind it
Documentation

/*When using the delete operator in strictNullChecks, 
the operand must now be any, unknown, never, or be optional 
(in that it contains undefined in the type). Otherwise, use of the delete operator is an error.*/

interface Thing {
  prop: string;
}

function f(x: Thing) {
  delete x.prop; // throws error = The operand of a 'delete' operator must be optional.
}

I am not able to realize the logic behind it

The logic as I understand is the following:

Interface Thing is a contract asking to have a (non-null, non-undefined) prop as a string .

If one removes the property, then the contract is not implemented anymore.

If you want it still valid when removed, just declare it as optional with a ? : prop?: string

I'm actually surprised that this was not causing error in earlier versions of TypeScript.

The logic behind of this, is that you need to implement your interface with an optional property like this:

interface Thing {
  prop?: string;
}
// OR
interface Thing {
  prop: string | undefined;
}

function f(x: Thing) {
  delete x.prop; 
}

So the interface's contract won't be broken.

Maybe this can helpful

const { propToDelete, ...otherProps} = youObject
return otherProps

with that you can use otherProps object without break out

Another implementation if you want it to exist:

interface Thing {
  prop: string;
}
interface PropoptionalThing {
  prop?: string;
}

function f(x: Thing): PropoptionalThing {
  let tmp: PropoptionalThing = x;
  delete tmp.prop;
  return tmp;
}

This is the new error that is coming in typescript code.

I am not able to realize the logic behind it
Documentation

/*When using the delete operator in strictNullChecks, 
the operand must now be any, unknown, never, or be optional 
(in that it contains undefined in the type). Otherwise, use of the delete operator is an error.*/

interface Thing {
  prop: string;
}

function f(x: Thing) {
  delete x.prop; // throws error = The operand of a 'delete' operator must be optional.
}

The prop property in Thing interface must be mark as optional using ? mark.

then your Thing interface must be like this.

interface Thing {
  prop?: string;
}

You could change the type of x to a partial:

function f(x: Partial<Thing>) {
  delete x.prop;
}

But I don't usually like to mutate (modify) objects which have been passed to me from possibly unknown code. So I would usually make a new object instead:

function f(x: Thing) {
  const y = { ...x } as Partial<Thing>;
  delete y.prop;
}

Since Partial makes all the properties optional, this will allow you to delete anything from y .

If you wanted to get more specific, you could use SetOptional from typesfest :

  const y = { ...x } as SetOptional<Thing, 'prop1' | 'prop2'>;

That would make prop1 and prop2 optional but keep all other properties mandatory (required).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM