Correctly check for 'delete' on optional properties in 'exactOptionalPropertyTypes'. (#44854)

* Check for optionality in 'delete'.

* Accepted baselines.
This commit is contained in:
Daniel Rosenwasser
2021-07-01 15:19:11 -07:00
committed by GitHub
parent 9d443b76aa
commit e3d6189e3a
2 changed files with 5 additions and 11 deletions
+4 -4
View File
@@ -31402,16 +31402,16 @@ namespace ts {
if (isReadonlySymbol(symbol)) {
error(expr, Diagnostics.The_operand_of_a_delete_operator_cannot_be_a_read_only_property);
}
checkDeleteExpressionMustBeOptional(expr, getTypeOfSymbol(symbol));
checkDeleteExpressionMustBeOptional(expr, symbol);
}
return booleanType;
}
function checkDeleteExpressionMustBeOptional(expr: AccessExpression, type: Type) {
function checkDeleteExpressionMustBeOptional(expr: AccessExpression, symbol: Symbol) {
const type = getTypeOfSymbol(symbol);
if (strictNullChecks &&
!(type.flags & (TypeFlags.AnyOrUnknown | TypeFlags.Never)) &&
!(exactOptionalPropertyTypes ? 0 : getFalsyFlags(type) & TypeFlags.Undefined)) {
!(exactOptionalPropertyTypes ? hasQuestionToken(symbol.valueDeclaration!) : getFalsyFlags(type) & TypeFlags.Undefined)) {
error(expr, Diagnostics.The_operand_of_a_delete_operator_must_be_optional);
}
}
@@ -1,13 +1,11 @@
tests/cases/compiler/deleteExpressionMustBeOptional_exactOptionalPropertyTypes.ts(25,8): error TS2790: The operand of a 'delete' operator must be optional.
tests/cases/compiler/deleteExpressionMustBeOptional_exactOptionalPropertyTypes.ts(26,8): error TS2790: The operand of a 'delete' operator must be optional.
tests/cases/compiler/deleteExpressionMustBeOptional_exactOptionalPropertyTypes.ts(27,8): error TS2790: The operand of a 'delete' operator must be optional.
tests/cases/compiler/deleteExpressionMustBeOptional_exactOptionalPropertyTypes.ts(28,8): error TS2790: The operand of a 'delete' operator must be optional.
tests/cases/compiler/deleteExpressionMustBeOptional_exactOptionalPropertyTypes.ts(29,8): error TS2790: The operand of a 'delete' operator must be optional.
tests/cases/compiler/deleteExpressionMustBeOptional_exactOptionalPropertyTypes.ts(30,8): error TS2790: The operand of a 'delete' operator must be optional.
tests/cases/compiler/deleteExpressionMustBeOptional_exactOptionalPropertyTypes.ts(34,10): error TS2339: Property 'j' does not exist on type 'Foo'.
==== tests/cases/compiler/deleteExpressionMustBeOptional_exactOptionalPropertyTypes.ts (7 errors) ====
==== tests/cases/compiler/deleteExpressionMustBeOptional_exactOptionalPropertyTypes.ts (5 errors) ====
interface Foo {
a: number
b: number | undefined
@@ -42,14 +40,10 @@ tests/cases/compiler/deleteExpressionMustBeOptional_exactOptionalPropertyTypes.t
~~~
!!! error TS2790: The operand of a 'delete' operator must be optional.
delete f.d
~~~
!!! error TS2790: The operand of a 'delete' operator must be optional.
delete f.e
~~~
!!! error TS2790: The operand of a 'delete' operator must be optional.
delete f.f
~~~
!!! error TS2790: The operand of a 'delete' operator must be optional.
delete f.g
delete f.h
delete f.i