Skip to content

Commit b90761c

Browse files
committed
Allow class properties and methods to be declared optional using '?'
1 parent 7706f38 commit b90761c

2 files changed

Lines changed: 8 additions & 13 deletions

File tree

src/compiler/checker.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3216,7 +3216,9 @@ namespace ts {
32163216
function getTypeOfFuncClassEnumModule(symbol: Symbol): Type {
32173217
const links = getSymbolLinks(symbol);
32183218
if (!links.type) {
3219-
links.type = createObjectType(TypeFlags.Anonymous, symbol);
3219+
const type = createObjectType(TypeFlags.Anonymous, symbol);
3220+
links.type = strictNullChecks && symbol.flags & SymbolFlags.Optional ?
3221+
addNullableKind(type, TypeFlags.Undefined) : type;
32203222
}
32213223
return links.type;
32223224
}
@@ -9921,7 +9923,8 @@ namespace ts {
99219923
}
99229924

99239925
const propType = getTypeOfSymbol(prop);
9924-
if (node.kind !== SyntaxKind.PropertyAccessExpression || !(prop.flags & (SymbolFlags.Variable | SymbolFlags.Property | SymbolFlags.Accessor)) || isAssignmentTarget(node)) {
9926+
if (node.kind !== SyntaxKind.PropertyAccessExpression || isAssignmentTarget(node) ||
9927+
!(propType.flags & TypeFlags.Union) && !(prop.flags & (SymbolFlags.Variable | SymbolFlags.Property | SymbolFlags.Accessor))) {
99259928
return propType;
99269929
}
99279930
const leftmostNode = getLeftmostIdentifierOrThis(node);
@@ -13396,7 +13399,7 @@ namespace ts {
1339613399

1339713400
// Abstract methods can't have an implementation -- in particular, they don't need one.
1339813401
if (!isExportSymbolInsideModule && lastSeenNonAmbientDeclaration && !lastSeenNonAmbientDeclaration.body &&
13399-
!(lastSeenNonAmbientDeclaration.flags & NodeFlags.Abstract)) {
13402+
!(lastSeenNonAmbientDeclaration.flags & NodeFlags.Abstract) && !lastSeenNonAmbientDeclaration.questionToken) {
1340013403
reportImplementationExpectedError(lastSeenNonAmbientDeclaration);
1340113404
}
1340213405

@@ -18290,7 +18293,7 @@ namespace ts {
1829018293
}
1829118294

1829218295
if (node.parent.kind === SyntaxKind.ObjectLiteralExpression) {
18293-
if (checkGrammarForInvalidQuestionMark(node, node.questionToken, Diagnostics.A_class_member_cannot_be_declared_optional)) {
18296+
if (checkGrammarForInvalidQuestionMark(node, node.questionToken, Diagnostics.An_object_member_cannot_be_declared_optional)) {
1829418297
return true;
1829518298
}
1829618299
else if (node.body === undefined) {
@@ -18299,9 +18302,6 @@ namespace ts {
1829918302
}
1830018303

1830118304
if (isClassLike(node.parent)) {
18302-
if (checkGrammarForInvalidQuestionMark(node, node.questionToken, Diagnostics.A_class_member_cannot_be_declared_optional)) {
18303-
return true;
18304-
}
1830518305
// Technically, computed properties in ambient contexts is disallowed
1830618306
// for property declarations and accessors too, not just methods.
1830718307
// However, property declarations disallow computed names in general,
@@ -18523,8 +18523,7 @@ namespace ts {
1852318523

1852418524
function checkGrammarProperty(node: PropertyDeclaration) {
1852518525
if (isClassLike(node.parent)) {
18526-
if (checkGrammarForInvalidQuestionMark(node, node.questionToken, Diagnostics.A_class_member_cannot_be_declared_optional) ||
18527-
checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_a_class_property_declaration_must_directly_refer_to_a_built_in_symbol)) {
18526+
if (checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_a_class_property_declaration_must_directly_refer_to_a_built_in_symbol)) {
1852818527
return true;
1852918528
}
1853018529
}

src/compiler/diagnosticMessages.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,6 @@
315315
"category": "Error",
316316
"code": 1110
317317
},
318-
"A class member cannot be declared optional.": {
319-
"category": "Error",
320-
"code": 1112
321-
},
322318
"A 'default' clause cannot appear more than once in a 'switch' statement.": {
323319
"category": "Error",
324320
"code": 1113

0 commit comments

Comments
 (0)