Skip to content

Commit e3d6189

Browse files
Correctly check for 'delete' on optional properties in 'exactOptionalPropertyTypes'. (#44854)
* Check for optionality in 'delete'. * Accepted baselines.
1 parent 9d443b7 commit e3d6189

File tree

2 files changed

+5
-11
lines changed

2 files changed

+5
-11
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31402,16 +31402,16 @@ namespace ts {
3140231402
if (isReadonlySymbol(symbol)) {
3140331403
error(expr, Diagnostics.The_operand_of_a_delete_operator_cannot_be_a_read_only_property);
3140431404
}
31405-
31406-
checkDeleteExpressionMustBeOptional(expr, getTypeOfSymbol(symbol));
31405+
checkDeleteExpressionMustBeOptional(expr, symbol);
3140731406
}
3140831407
return booleanType;
3140931408
}
3141031409

31411-
function checkDeleteExpressionMustBeOptional(expr: AccessExpression, type: Type) {
31410+
function checkDeleteExpressionMustBeOptional(expr: AccessExpression, symbol: Symbol) {
31411+
const type = getTypeOfSymbol(symbol);
3141231412
if (strictNullChecks &&
3141331413
!(type.flags & (TypeFlags.AnyOrUnknown | TypeFlags.Never)) &&
31414-
!(exactOptionalPropertyTypes ? 0 : getFalsyFlags(type) & TypeFlags.Undefined)) {
31414+
!(exactOptionalPropertyTypes ? hasQuestionToken(symbol.valueDeclaration!) : getFalsyFlags(type) & TypeFlags.Undefined)) {
3141531415
error(expr, Diagnostics.The_operand_of_a_delete_operator_must_be_optional);
3141631416
}
3141731417
}

tests/baselines/reference/deleteExpressionMustBeOptional_exactOptionalPropertyTypes(exactoptionalpropertytypes=true).errors.txt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
tests/cases/compiler/deleteExpressionMustBeOptional_exactOptionalPropertyTypes.ts(25,8): error TS2790: The operand of a 'delete' operator must be optional.
22
tests/cases/compiler/deleteExpressionMustBeOptional_exactOptionalPropertyTypes.ts(26,8): error TS2790: The operand of a 'delete' operator must be optional.
33
tests/cases/compiler/deleteExpressionMustBeOptional_exactOptionalPropertyTypes.ts(27,8): error TS2790: The operand of a 'delete' operator must be optional.
4-
tests/cases/compiler/deleteExpressionMustBeOptional_exactOptionalPropertyTypes.ts(28,8): error TS2790: The operand of a 'delete' operator must be optional.
54
tests/cases/compiler/deleteExpressionMustBeOptional_exactOptionalPropertyTypes.ts(29,8): error TS2790: The operand of a 'delete' operator must be optional.
6-
tests/cases/compiler/deleteExpressionMustBeOptional_exactOptionalPropertyTypes.ts(30,8): error TS2790: The operand of a 'delete' operator must be optional.
75
tests/cases/compiler/deleteExpressionMustBeOptional_exactOptionalPropertyTypes.ts(34,10): error TS2339: Property 'j' does not exist on type 'Foo'.
86

97

10-
==== tests/cases/compiler/deleteExpressionMustBeOptional_exactOptionalPropertyTypes.ts (7 errors) ====
8+
==== tests/cases/compiler/deleteExpressionMustBeOptional_exactOptionalPropertyTypes.ts (5 errors) ====
119
interface Foo {
1210
a: number
1311
b: number | undefined
@@ -42,14 +40,10 @@ tests/cases/compiler/deleteExpressionMustBeOptional_exactOptionalPropertyTypes.t
4240
~~~
4341
!!! error TS2790: The operand of a 'delete' operator must be optional.
4442
delete f.d
45-
~~~
46-
!!! error TS2790: The operand of a 'delete' operator must be optional.
4743
delete f.e
4844
~~~
4945
!!! error TS2790: The operand of a 'delete' operator must be optional.
5046
delete f.f
51-
~~~
52-
!!! error TS2790: The operand of a 'delete' operator must be optional.
5347
delete f.g
5448
delete f.h
5549
delete f.i

0 commit comments

Comments
 (0)