@@ -9083,7 +9083,7 @@ namespace ts {
9083
9083
return getTypeForBindingElement(declaration as BindingElement);
9084
9084
}
9085
9085
9086
- const isProperty = isPropertyDeclaration(declaration) || isPropertySignature(declaration);
9086
+ const isProperty = isPropertyDeclaration(declaration) && !hasAccessorModifier(declaration) || isPropertySignature(declaration);
9087
9087
const isOptional = includeOptionality && (
9088
9088
isProperty && !!declaration.questionToken ||
9089
9089
isParameter(declaration) && (!!declaration.questionToken || isJSDocOptionalParameter(declaration)) ||
@@ -9831,21 +9831,25 @@ namespace ts {
9831
9831
return type;
9832
9832
}
9833
9833
9834
- function getAnnotatedAccessorTypeNode(accessor: AccessorDeclaration | undefined): TypeNode | undefined {
9834
+ function getAnnotatedAccessorTypeNode(accessor: AccessorDeclaration | PropertyDeclaration | undefined): TypeNode | undefined {
9835
9835
if (accessor) {
9836
- if (accessor.kind === SyntaxKind.GetAccessor) {
9837
- const getterTypeAnnotation = getEffectiveReturnTypeNode(accessor);
9838
- return getterTypeAnnotation;
9839
- }
9840
- else {
9841
- const setterTypeAnnotation = getEffectiveSetAccessorTypeAnnotationNode(accessor);
9842
- return setterTypeAnnotation;
9836
+ switch (accessor.kind) {
9837
+ case SyntaxKind.GetAccessor:
9838
+ const getterTypeAnnotation = getEffectiveReturnTypeNode(accessor);
9839
+ return getterTypeAnnotation;
9840
+ case SyntaxKind.SetAccessor:
9841
+ const setterTypeAnnotation = getEffectiveSetAccessorTypeAnnotationNode(accessor);
9842
+ return setterTypeAnnotation;
9843
+ case SyntaxKind.PropertyDeclaration:
9844
+ Debug.assert(hasAccessorModifier(accessor));
9845
+ const accessorTypeAnnotation = getEffectiveTypeAnnotationNode(accessor);
9846
+ return accessorTypeAnnotation;
9843
9847
}
9844
9848
}
9845
9849
return undefined;
9846
9850
}
9847
9851
9848
- function getAnnotatedAccessorType(accessor: AccessorDeclaration | undefined): Type | undefined {
9852
+ function getAnnotatedAccessorType(accessor: AccessorDeclaration | PropertyDeclaration | undefined): Type | undefined {
9849
9853
const node = getAnnotatedAccessorTypeNode(accessor);
9850
9854
return node && getTypeFromTypeNode(node);
9851
9855
}
@@ -9867,19 +9871,26 @@ namespace ts {
9867
9871
}
9868
9872
const getter = getDeclarationOfKind<AccessorDeclaration>(symbol, SyntaxKind.GetAccessor);
9869
9873
const setter = getDeclarationOfKind<AccessorDeclaration>(symbol, SyntaxKind.SetAccessor);
9874
+ const accessor = tryCast(getDeclarationOfKind<PropertyDeclaration>(symbol, SyntaxKind.PropertyDeclaration), isAutoAccessorPropertyDeclaration);
9875
+
9870
9876
// We try to resolve a getter type annotation, a setter type annotation, or a getter function
9871
9877
// body return type inference, in that order.
9872
9878
let type = getter && isInJSFile(getter) && getTypeForDeclarationFromJSDocComment(getter) ||
9873
9879
getAnnotatedAccessorType(getter) ||
9874
9880
getAnnotatedAccessorType(setter) ||
9875
- getter && getter.body && getReturnTypeFromBody(getter);
9881
+ getAnnotatedAccessorType(accessor) ||
9882
+ getter && getter.body && getReturnTypeFromBody(getter) ||
9883
+ accessor && accessor.initializer && getWidenedTypeForVariableLikeDeclaration(accessor, /*includeOptionality*/ true);
9876
9884
if (!type) {
9877
9885
if (setter && !isPrivateWithinAmbient(setter)) {
9878
9886
errorOrSuggestion(noImplicitAny, setter, Diagnostics.Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_parameter_type_annotation, symbolToString(symbol));
9879
9887
}
9880
9888
else if (getter && !isPrivateWithinAmbient(getter)) {
9881
9889
errorOrSuggestion(noImplicitAny, getter, Diagnostics.Property_0_implicitly_has_type_any_because_its_get_accessor_lacks_a_return_type_annotation, symbolToString(symbol));
9882
9890
}
9891
+ else if (accessor && !isPrivateWithinAmbient(accessor)) {
9892
+ errorOrSuggestion(noImplicitAny, accessor, Diagnostics.Member_0_implicitly_has_an_1_type, symbolToString(symbol), "any");
9893
+ }
9883
9894
type = anyType;
9884
9895
}
9885
9896
if (!popTypeResolution()) {
@@ -9889,6 +9900,9 @@ namespace ts {
9889
9900
else if (getAnnotatedAccessorTypeNode(setter)) {
9890
9901
error(setter, Diagnostics._0_is_referenced_directly_or_indirectly_in_its_own_type_annotation, symbolToString(symbol));
9891
9902
}
9903
+ else if (getAnnotatedAccessorTypeNode(accessor)) {
9904
+ error(setter, Diagnostics._0_is_referenced_directly_or_indirectly_in_its_own_type_annotation, symbolToString(symbol));
9905
+ }
9892
9906
else if (getter && noImplicitAny) {
9893
9907
error(getter, Diagnostics._0_implicitly_has_return_type_any_because_it_does_not_have_a_return_type_annotation_and_is_referenced_directly_or_indirectly_in_one_of_its_return_expressions, symbolToString(symbol));
9894
9908
}
@@ -9905,7 +9919,9 @@ namespace ts {
9905
9919
if (!pushTypeResolution(symbol, TypeSystemPropertyName.WriteType)) {
9906
9920
return errorType;
9907
9921
}
9908
- const setter = getDeclarationOfKind<AccessorDeclaration>(symbol, SyntaxKind.SetAccessor);
9922
+
9923
+ const setter = getDeclarationOfKind<AccessorDeclaration>(symbol, SyntaxKind.SetAccessor)
9924
+ ?? tryCast(getDeclarationOfKind<PropertyDeclaration>(symbol, SyntaxKind.PropertyDeclaration), isAutoAccessorPropertyDeclaration);
9909
9925
let writeType = getAnnotatedAccessorType(setter);
9910
9926
if (!popTypeResolution()) {
9911
9927
if (getAnnotatedAccessorTypeNode(setter)) {
@@ -11063,8 +11079,10 @@ namespace ts {
11063
11079
const members = getMembersOfDeclaration(decl);
11064
11080
if (members) {
11065
11081
for (const member of members) {
11066
- if (isStatic === hasStaticModifier(member) && hasLateBindableName(member)) {
11067
- lateBindMember(symbol, earlySymbols, lateSymbols, member);
11082
+ if (isStatic === hasStaticModifier(member)) {
11083
+ if (hasLateBindableName(member)) {
11084
+ lateBindMember(symbol, earlySymbols, lateSymbols, member);
11085
+ }
11068
11086
}
11069
11087
}
11070
11088
}
@@ -11078,8 +11096,10 @@ namespace ts {
11078
11096
|| isBinaryExpression(member) && isPossiblyAliasedThisProperty(member, assignmentKind)
11079
11097
|| assignmentKind === AssignmentDeclarationKind.ObjectDefinePrototypeProperty
11080
11098
|| assignmentKind === AssignmentDeclarationKind.Prototype; // A straight `Prototype` assignment probably can never have a computed name
11081
- if (isStatic === !isInstanceMember && hasLateBindableName(member)) {
11082
- lateBindMember(symbol, earlySymbols, lateSymbols, member);
11099
+ if (isStatic === !isInstanceMember) {
11100
+ if (hasLateBindableName(member)) {
11101
+ lateBindMember(symbol, earlySymbols, lateSymbols, member);
11102
+ }
11083
11103
}
11084
11104
}
11085
11105
}
@@ -12924,7 +12944,7 @@ namespace ts {
12924
12944
}
12925
12945
12926
12946
function isOptionalPropertyDeclaration(node: Declaration) {
12927
- return isPropertyDeclaration(node) && node.questionToken;
12947
+ return isPropertyDeclaration(node) && !hasAccessorModifier(node) && node.questionToken;
12928
12948
}
12929
12949
12930
12950
function isOptionalJSDocPropertyLikeTag(node: Node): node is JSDocPropertyLikeTag {
@@ -30759,7 +30779,7 @@ namespace ts {
30759
30779
// A method or accessor declaration decorator will have two or three arguments (see
30760
30780
// `PropertyDecorator` and `MethodDecorator` in core.d.ts). If we are emitting decorators
30761
30781
// for ES3, we will only pass two arguments.
30762
- const hasPropDesc = parent.kind !== SyntaxKind.PropertyDeclaration && languageVersion !== ScriptTarget.ES3 ;
30782
+ const hasPropDesc = languageVersion !== ScriptTarget.ES3 && (!isPropertyDeclaration(parent) || hasAccessorModifier(parent)) ;
30763
30783
return [
30764
30784
createSyntheticExpression(expr, getParentTypeOfClassElement(parent as ClassElement)),
30765
30785
createSyntheticExpression(expr, getClassElementPropertyKeyType(parent as ClassElement)),
@@ -30778,7 +30798,7 @@ namespace ts {
30778
30798
case SyntaxKind.ClassExpression:
30779
30799
return 1;
30780
30800
case SyntaxKind.PropertyDeclaration:
30781
- return 2;
30801
+ return hasAccessorModifier(node.parent) ? 3 : 2;
30782
30802
case SyntaxKind.MethodDeclaration:
30783
30803
case SyntaxKind.GetAccessor:
30784
30804
case SyntaxKind.SetAccessor:
@@ -44189,6 +44209,9 @@ namespace ts {
44189
44209
else if (flags & ModifierFlags.Readonly) {
44190
44210
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_must_precede_1_modifier, "override", "readonly");
44191
44211
}
44212
+ else if (flags & ModifierFlags.Accessor) {
44213
+ return grammarErrorOnNode(modifier, Diagnostics._0_modifier_must_precede_1_modifier, "override", "accessor");
44214
+ }
44192
44215
else if (flags & ModifierFlags.Async) {
44193
44216
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_must_precede_1_modifier, "override", "async");
44194
44217
}
@@ -44210,6 +44233,9 @@ namespace ts {
44210
44233
else if (flags & ModifierFlags.Static) {
44211
44234
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_must_precede_1_modifier, text, "static");
44212
44235
}
44236
+ else if (flags & ModifierFlags.Accessor) {
44237
+ return grammarErrorOnNode(modifier, Diagnostics._0_modifier_must_precede_1_modifier, text, "accessor");
44238
+ }
44213
44239
else if (flags & ModifierFlags.Readonly) {
44214
44240
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_must_precede_1_modifier, text, "readonly");
44215
44241
}
@@ -44243,6 +44269,9 @@ namespace ts {
44243
44269
else if (flags & ModifierFlags.Async) {
44244
44270
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_must_precede_1_modifier, "static", "async");
44245
44271
}
44272
+ else if (flags & ModifierFlags.Accessor) {
44273
+ return grammarErrorOnNode(modifier, Diagnostics._0_modifier_must_precede_1_modifier, "static", "accessor");
44274
+ }
44246
44275
else if (node.parent.kind === SyntaxKind.ModuleBlock || node.parent.kind === SyntaxKind.SourceFile) {
44247
44276
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_appear_on_a_module_or_namespace_element, "static");
44248
44277
}
@@ -44259,6 +44288,23 @@ namespace ts {
44259
44288
lastStatic = modifier;
44260
44289
break;
44261
44290
44291
+ case SyntaxKind.AccessorKeyword:
44292
+ if (flags & ModifierFlags.Accessor) {
44293
+ return grammarErrorOnNode(modifier, Diagnostics._0_modifier_already_seen, "accessor");
44294
+ }
44295
+ else if (flags & ModifierFlags.Readonly) {
44296
+ return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "accessor", "readonly");
44297
+ }
44298
+ else if (flags & ModifierFlags.Ambient) {
44299
+ return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "accessor", "declare");
44300
+ }
44301
+ else if (node.kind !== SyntaxKind.PropertyDeclaration) {
44302
+ return grammarErrorOnNode(modifier, Diagnostics.accessor_modifier_can_only_appear_on_a_property_declaration);
44303
+ }
44304
+
44305
+ flags |= ModifierFlags.Accessor;
44306
+ break;
44307
+
44262
44308
case SyntaxKind.ReadonlyKeyword:
44263
44309
if (flags & ModifierFlags.Readonly) {
44264
44310
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_already_seen, "readonly");
@@ -44355,6 +44401,9 @@ namespace ts {
44355
44401
if (flags & ModifierFlags.Override) {
44356
44402
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_must_precede_1_modifier, "abstract", "override");
44357
44403
}
44404
+ if (flags & ModifierFlags.Accessor) {
44405
+ return grammarErrorOnNode(modifier, Diagnostics._0_modifier_must_precede_1_modifier, "abstract", "accessor");
44406
+ }
44358
44407
}
44359
44408
if (isNamedDeclaration(node) && node.name.kind === SyntaxKind.PrivateIdentifier) {
44360
44409
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_be_used_with_a_private_identifier, "abstract");
@@ -45553,6 +45602,12 @@ namespace ts {
45553
45602
if (languageVersion < ScriptTarget.ES2015 && isPrivateIdentifier(node.name)) {
45554
45603
return grammarErrorOnNode(node.name, Diagnostics.Private_identifiers_are_only_available_when_targeting_ECMAScript_2015_and_higher);
45555
45604
}
45605
+ if (languageVersion < ScriptTarget.ES2015 && isAutoAccessorPropertyDeclaration(node)) {
45606
+ return grammarErrorOnNode(node.name, Diagnostics.Properties_with_the_accessor_modifier_are_only_available_when_targeting_ECMAScript_2015_and_higher);
45607
+ }
45608
+ if (isAutoAccessorPropertyDeclaration(node) && checkGrammarForInvalidQuestionMark(node.questionToken, Diagnostics.An_accessor_property_cannot_be_declared_optional)) {
45609
+ return true;
45610
+ }
45556
45611
}
45557
45612
else if (node.parent.kind === SyntaxKind.InterfaceDeclaration) {
45558
45613
if (checkGrammarForInvalidDynamicName(node.name, Diagnostics.A_computed_property_name_in_an_interface_must_refer_to_an_expression_whose_type_is_a_literal_type_or_a_unique_symbol_type)) {
0 commit comments