@@ -4501,6 +4501,8 @@ namespace ts {
4501
4501
}
4502
4502
4503
4503
function signatureToSignatureDeclarationHelper(signature: Signature, kind: SyntaxKind, context: NodeBuilderContext): SignatureDeclaration {
4504
+ const suppressAny = context.flags & NodeBuilderFlags.SuppressAnyReturnType;
4505
+ if (suppressAny) context.flags &= ~NodeBuilderFlags.SuppressAnyReturnType; // suppress only toplevel `any`s
4504
4506
let typeParameters: TypeParameterDeclaration[] | undefined;
4505
4507
let typeArguments: TypeNode[] | undefined;
4506
4508
if (context.flags & NodeBuilderFlags.WriteTypeArgumentsOfSignature && signature.target && signature.mapper && signature.target.typeParameters) {
@@ -4530,15 +4532,12 @@ namespace ts {
4530
4532
}
4531
4533
else {
4532
4534
const returnType = getReturnTypeOfSignature(signature);
4533
- returnTypeNode = returnType && typeToTypeNodeHelper(returnType, context);
4534
- }
4535
- if (context.flags & NodeBuilderFlags.SuppressAnyReturnType) {
4536
- if (returnTypeNode && returnTypeNode.kind === SyntaxKind.AnyKeyword ) {
4537
- returnTypeNode = undefined ;
4535
+ if ( returnType && !(suppressAny && isTypeAny(returnType))) {
4536
+ returnTypeNode = typeToTypeNodeHelper(returnType, context);
4537
+ }
4538
+ else if (!suppressAny ) {
4539
+ returnTypeNode = createKeywordTypeNode(SyntaxKind.AnyKeyword) ;
4538
4540
}
4539
- }
4540
- else if (!returnTypeNode) {
4541
- returnTypeNode = createKeywordTypeNode(SyntaxKind.AnyKeyword);
4542
4541
}
4543
4542
context.approximateLength += 3; // Usually a signature contributes a few more characters than this, but 3 is the minimum
4544
4543
return createSignatureDeclaration(kind, typeParameters, parameters, returnTypeNode, typeArguments);
@@ -14410,7 +14409,8 @@ namespace ts {
14410
14409
}
14411
14410
14412
14411
function isEmptyResolvedType(t: ResolvedType) {
14413
- return t.properties.length === 0 &&
14412
+ return t !== anyFunctionType &&
14413
+ t.properties.length === 0 &&
14414
14414
t.callSignatures.length === 0 &&
14415
14415
t.constructSignatures.length === 0 &&
14416
14416
!t.stringIndexInfo &&
@@ -19045,7 +19045,7 @@ namespace ts {
19045
19045
return getTypeOfSymbol(symbol);
19046
19046
}
19047
19047
if (diagnostic && symbol.valueDeclaration) {
19048
- addRelatedInfo(diagnostic, createDiagnosticForNode(symbol.valueDeclaration, Diagnostics._0_is_declared_here , symbolToString(symbol)));
19048
+ addRelatedInfo(diagnostic, createDiagnosticForNode(symbol.valueDeclaration, Diagnostics._0_needs_an_explicit_type_annotation , symbolToString(symbol)));
19049
19049
}
19050
19050
}
19051
19051
}
@@ -19777,13 +19777,22 @@ namespace ts {
19777
19777
}
19778
19778
19779
19779
function narrowTypeByOptionalChainContainment(type: Type, operator: SyntaxKind, value: Expression, assumeTrue: boolean): Type {
19780
- // We are in a branch of obj?.foo === value or obj?.foo !== value. We remove undefined and null from
19781
- // the type of obj if (a) the operator is === and the type of value doesn't include undefined or (b) the
19782
- // operator is !== and the type of value is undefined.
19783
- const effectiveTrue = operator === SyntaxKind.EqualsEqualsToken || operator === SyntaxKind.EqualsEqualsEqualsToken ? assumeTrue : !assumeTrue;
19784
- const doubleEquals = operator === SyntaxKind.EqualsEqualsToken || operator === SyntaxKind.ExclamationEqualsToken;
19785
- const valueNonNullish = !(getTypeFacts(getTypeOfExpression(value)) & (doubleEquals ? TypeFacts.EQUndefinedOrNull : TypeFacts.EQUndefined));
19786
- return effectiveTrue === valueNonNullish ? getTypeWithFacts(type, TypeFacts.NEUndefinedOrNull) : type;
19780
+ // We are in a branch of obj?.foo === value (or any one of the other equality operators). We narrow obj as follows:
19781
+ // When operator is === and type of value excludes undefined, null and undefined is removed from type of obj in true branch.
19782
+ // When operator is !== and type of value excludes undefined, null and undefined is removed from type of obj in false branch.
19783
+ // When operator is == and type of value excludes null and undefined, null and undefined is removed from type of obj in true branch.
19784
+ // When operator is != and type of value excludes null and undefined, null and undefined is removed from type of obj in false branch.
19785
+ // When operator is === and type of value is undefined, null and undefined is removed from type of obj in false branch.
19786
+ // When operator is !== and type of value is undefined, null and undefined is removed from type of obj in true branch.
19787
+ // When operator is == and type of value is null or undefined, null and undefined is removed from type of obj in false branch.
19788
+ // When operator is != and type of value is null or undefined, null and undefined is removed from type of obj in true branch.
19789
+ const equalsOperator = operator === SyntaxKind.EqualsEqualsToken || operator === SyntaxKind.EqualsEqualsEqualsToken;
19790
+ const nullableFlags = operator === SyntaxKind.EqualsEqualsToken || operator === SyntaxKind.ExclamationEqualsToken ? TypeFlags.Nullable : TypeFlags.Undefined;
19791
+ const valueType = getTypeOfExpression(value);
19792
+ // Note that we include any and unknown in the exclusion test because their domain includes null and undefined.
19793
+ const removeNullable = equalsOperator !== assumeTrue && everyType(valueType, t => !!(t.flags & nullableFlags)) ||
19794
+ equalsOperator === assumeTrue && everyType(valueType, t => !(t.flags & (TypeFlags.AnyOrUnknown | nullableFlags)));
19795
+ return removeNullable ? getTypeWithFacts(type, TypeFacts.NEUndefinedOrNull) : type;
19787
19796
}
19788
19797
19789
19798
function narrowTypeByEquality(type: Type, operator: SyntaxKind, value: Expression, assumeTrue: boolean): Type {
@@ -23234,10 +23243,6 @@ namespace ts {
23234
23243
const assignmentKind = getAssignmentTargetKind(node);
23235
23244
const apparentType = getApparentType(assignmentKind !== AssignmentKind.None || isMethodAccessForCall(node) ? getWidenedType(leftType) : leftType);
23236
23245
if (isPrivateIdentifier(right)) {
23237
- if (isOptionalChain(node)) {
23238
- grammarErrorOnNode(right, Diagnostics.An_optional_chain_cannot_contain_private_identifiers);
23239
- return anyType;
23240
- }
23241
23246
checkExternalEmitHelpers(node, ExternalEmitHelpers.ClassPrivateFieldGet);
23242
23247
}
23243
23248
const isAnyLike = isTypeAny(apparentType) || apparentType === silentNeverType;
@@ -32417,7 +32422,7 @@ namespace ts {
32417
32422
}
32418
32423
if (isInstancePropertyWithoutInitializer(member)) {
32419
32424
const propName = (<PropertyDeclaration>member).name;
32420
- if (isIdentifier(propName)) {
32425
+ if (isIdentifier(propName) || isPrivateIdentifier(propName) ) {
32421
32426
const type = getTypeOfSymbol(getSymbolOfNode(member));
32422
32427
if (!(type.flags & TypeFlags.AnyOrUnknown || getFalsyFlags(type) & TypeFlags.Undefined)) {
32423
32428
if (!constructor || !isPropertyInitializedInConstructor(propName, type, constructor)) {
@@ -32436,7 +32441,7 @@ namespace ts {
32436
32441
!(<PropertyDeclaration>node).initializer;
32437
32442
}
32438
32443
32439
- function isPropertyInitializedInConstructor(propName: Identifier, propType: Type, constructor: ConstructorDeclaration) {
32444
+ function isPropertyInitializedInConstructor(propName: Identifier | PrivateIdentifier , propType: Type, constructor: ConstructorDeclaration) {
32440
32445
const reference = createPropertyAccess(createThis(), propName);
32441
32446
reference.expression.parent = reference;
32442
32447
reference.parent = constructor;
@@ -35972,30 +35977,32 @@ namespace ts {
35972
35977
throw Debug.assertNever(prop, "Unexpected syntax kind:" + (<Node>prop).kind);
35973
35978
}
35974
35979
35975
- const effectiveName = getPropertyNameForPropertyNameNode(name);
35976
- if (effectiveName === undefined) {
35977
- continue;
35978
- }
35980
+ if (!inDestructuring) {
35981
+ const effectiveName = getPropertyNameForPropertyNameNode(name);
35982
+ if (effectiveName === undefined) {
35983
+ continue;
35984
+ }
35979
35985
35980
- const existingKind = seen.get(effectiveName);
35981
- if (!existingKind) {
35982
- seen.set(effectiveName, currentKind);
35983
- }
35984
- else {
35985
- if ((currentKind & DeclarationMeaning.PropertyAssignmentOrMethod) && (existingKind & DeclarationMeaning.PropertyAssignmentOrMethod)) {
35986
- grammarErrorOnNode(name, Diagnostics.Duplicate_identifier_0, getTextOfNode(name));
35986
+ const existingKind = seen.get(effectiveName);
35987
+ if (!existingKind) {
35988
+ seen.set(effectiveName, currentKind);
35987
35989
}
35988
- else if ((currentKind & DeclarationMeaning.GetOrSetAccessor) && (existingKind & DeclarationMeaning.GetOrSetAccessor)) {
35989
- if (existingKind !== DeclarationMeaning.GetOrSetAccessor && currentKind !== existingKind) {
35990
- seen.set(effectiveName, currentKind | existingKind);
35990
+ else {
35991
+ if ((currentKind & DeclarationMeaning.PropertyAssignmentOrMethod) && (existingKind & DeclarationMeaning.PropertyAssignmentOrMethod)) {
35992
+ grammarErrorOnNode(name, Diagnostics.Duplicate_identifier_0, getTextOfNode(name));
35993
+ }
35994
+ else if ((currentKind & DeclarationMeaning.GetOrSetAccessor) && (existingKind & DeclarationMeaning.GetOrSetAccessor)) {
35995
+ if (existingKind !== DeclarationMeaning.GetOrSetAccessor && currentKind !== existingKind) {
35996
+ seen.set(effectiveName, currentKind | existingKind);
35997
+ }
35998
+ else {
35999
+ return grammarErrorOnNode(name, Diagnostics.An_object_literal_cannot_have_multiple_get_Slashset_accessors_with_the_same_name);
36000
+ }
35991
36001
}
35992
36002
else {
35993
- return grammarErrorOnNode(name, Diagnostics.An_object_literal_cannot_have_multiple_get_Slashset_accessors_with_the_same_name );
36003
+ return grammarErrorOnNode(name, Diagnostics.An_object_literal_cannot_have_property_and_accessor_with_the_same_name );
35994
36004
}
35995
36005
}
35996
- else {
35997
- return grammarErrorOnNode(name, Diagnostics.An_object_literal_cannot_have_property_and_accessor_with_the_same_name);
35998
- }
35999
36006
}
36000
36007
}
36001
36008
}
0 commit comments