Fix double error on invalid delete of readonly property (#55449)

This commit is contained in:
Shinichi Katayama
2023-08-24 09:57:22 -07:00
committed by GitHub
parent fecbae5d2e
commit 98d7e0b936
6 changed files with 63 additions and 1 deletions

View File

@@ -36713,7 +36713,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (isReadonlySymbol(symbol)) {
error(expr, Diagnostics.The_operand_of_a_delete_operator_cannot_be_a_read_only_property);
}
checkDeleteExpressionMustBeOptional(expr, symbol);
else {
checkDeleteExpressionMustBeOptional(expr, symbol);
}
}
return booleanType;
}

View File

@@ -0,0 +1,10 @@
deleteReadonlyInStrictNullChecks.ts(3,8): error TS2704: The operand of a 'delete' operator cannot be a read-only property.
==== deleteReadonlyInStrictNullChecks.ts (1 errors) ====
interface Function { readonly name: string; }
class Foo {}
delete Foo.name;
~~~~~~~~
!!! error TS2704: The operand of a 'delete' operator cannot be a read-only property.

View File

@@ -0,0 +1,15 @@
//// [tests/cases/compiler/deleteReadonlyInStrictNullChecks.ts] ////
//// [deleteReadonlyInStrictNullChecks.ts]
interface Function { readonly name: string; }
class Foo {}
delete Foo.name;
//// [deleteReadonlyInStrictNullChecks.js]
var Foo = /** @class */ (function () {
function Foo() {
}
return Foo;
}());
delete Foo.name;

View File

@@ -0,0 +1,15 @@
//// [tests/cases/compiler/deleteReadonlyInStrictNullChecks.ts] ////
=== deleteReadonlyInStrictNullChecks.ts ===
interface Function { readonly name: string; }
>Function : Symbol(Function, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(deleteReadonlyInStrictNullChecks.ts, 0, 0))
>name : Symbol(Function.name, Decl(deleteReadonlyInStrictNullChecks.ts, 0, 20))
class Foo {}
>Foo : Symbol(Foo, Decl(deleteReadonlyInStrictNullChecks.ts, 0, 45))
delete Foo.name;
>Foo.name : Symbol(Function.name, Decl(deleteReadonlyInStrictNullChecks.ts, 0, 20))
>Foo : Symbol(Foo, Decl(deleteReadonlyInStrictNullChecks.ts, 0, 45))
>name : Symbol(Function.name, Decl(deleteReadonlyInStrictNullChecks.ts, 0, 20))

View File

@@ -0,0 +1,15 @@
//// [tests/cases/compiler/deleteReadonlyInStrictNullChecks.ts] ////
=== deleteReadonlyInStrictNullChecks.ts ===
interface Function { readonly name: string; }
>name : string
class Foo {}
>Foo : Foo
delete Foo.name;
>delete Foo.name : boolean
>Foo.name : string
>Foo : typeof Foo
>name : string

View File

@@ -0,0 +1,5 @@
// @strictNullChecks: true
interface Function { readonly name: string; }
class Foo {}
delete Foo.name;