Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1K
Views
¿Cuál es la lógica detrás del error de TypeScript "El operando de un operador 'eliminar' debe ser opcional"?

Este es el nuevo error que viene en código mecanografiado.

No soy capaz de darme cuenta de la lógica detrás de esto.
Documentación

 /*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. }
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

No soy capaz de darme cuenta de la lógica detrás de esto.

La lógica según entiendo es la siguiente:

Interface Thing es un contrato que solicita tener un prop (no nulo, no indefinido) como una string .

Si uno quita la propiedad, entonces el contrato ya no se implementa.

Si desea que siga siendo válido cuando se elimine, simplemente declárelo como opcional con un ? : prop?: string

De hecho, estoy sorprendido de que esto no haya causado un error en versiones anteriores de TypeScript.

over 4 years ago · Santiago Trujillo Report

0

La lógica detrás de esto es que necesita implementar su interfaz con una propiedad opcional como esta:

 interface Thing { prop?: string; } // OR interface Thing { prop: string | undefined; } function f(x: Thing) { delete x.prop; }

Entonces el contrato de la interfaz no se romperá.

over 4 years ago · Santiago Trujillo Report

0

Otra implementación si quieres que exista:

 interface Thing { prop: string; } interface PropoptionalThing { prop?: string; } function f(x: Thing): PropoptionalThing { let tmp: PropoptionalThing = x; delete tmp.prop; return tmp; }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!