@@ -4501,6 +4501,8 @@ namespace ts {
45014501 }
45024502
45034503 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
45044506 let typeParameters: TypeParameterDeclaration[] | undefined;
45054507 let typeArguments: TypeNode[] | undefined;
45064508 if (context.flags & NodeBuilderFlags.WriteTypeArgumentsOfSignature && signature.target && signature.mapper && signature.target.typeParameters) {
@@ -4530,15 +4532,12 @@ namespace ts {
45304532 }
45314533 else {
45324534 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) ;
45384540 }
4539- }
4540- else if (!returnTypeNode) {
4541- returnTypeNode = createKeywordTypeNode(SyntaxKind.AnyKeyword);
45424541 }
45434542 context.approximateLength += 3; // Usually a signature contributes a few more characters than this, but 3 is the minimum
45444543 return createSignatureDeclaration(kind, typeParameters, parameters, returnTypeNode, typeArguments);
@@ -14410,7 +14409,8 @@ namespace ts {
1441014409 }
1441114410
1441214411 function isEmptyResolvedType(t: ResolvedType) {
14413- return t.properties.length === 0 &&
14412+ return t !== anyFunctionType &&
14413+ t.properties.length === 0 &&
1441414414 t.callSignatures.length === 0 &&
1441514415 t.constructSignatures.length === 0 &&
1441614416 !t.stringIndexInfo &&
@@ -19045,7 +19045,7 @@ namespace ts {
1904519045 return getTypeOfSymbol(symbol);
1904619046 }
1904719047 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)));
1904919049 }
1905019050 }
1905119051 }
@@ -19777,13 +19777,22 @@ namespace ts {
1977719777 }
1977819778
1977919779 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;
1978719796 }
1978819797
1978919798 function narrowTypeByEquality(type: Type, operator: SyntaxKind, value: Expression, assumeTrue: boolean): Type {
@@ -23234,10 +23243,6 @@ namespace ts {
2323423243 const assignmentKind = getAssignmentTargetKind(node);
2323523244 const apparentType = getApparentType(assignmentKind !== AssignmentKind.None || isMethodAccessForCall(node) ? getWidenedType(leftType) : leftType);
2323623245 if (isPrivateIdentifier(right)) {
23237- if (isOptionalChain(node)) {
23238- grammarErrorOnNode(right, Diagnostics.An_optional_chain_cannot_contain_private_identifiers);
23239- return anyType;
23240- }
2324123246 checkExternalEmitHelpers(node, ExternalEmitHelpers.ClassPrivateFieldGet);
2324223247 }
2324323248 const isAnyLike = isTypeAny(apparentType) || apparentType === silentNeverType;
@@ -32417,7 +32422,7 @@ namespace ts {
3241732422 }
3241832423 if (isInstancePropertyWithoutInitializer(member)) {
3241932424 const propName = (<PropertyDeclaration>member).name;
32420- if (isIdentifier(propName)) {
32425+ if (isIdentifier(propName) || isPrivateIdentifier(propName) ) {
3242132426 const type = getTypeOfSymbol(getSymbolOfNode(member));
3242232427 if (!(type.flags & TypeFlags.AnyOrUnknown || getFalsyFlags(type) & TypeFlags.Undefined)) {
3242332428 if (!constructor || !isPropertyInitializedInConstructor(propName, type, constructor)) {
@@ -32436,7 +32441,7 @@ namespace ts {
3243632441 !(<PropertyDeclaration>node).initializer;
3243732442 }
3243832443
32439- function isPropertyInitializedInConstructor(propName: Identifier, propType: Type, constructor: ConstructorDeclaration) {
32444+ function isPropertyInitializedInConstructor(propName: Identifier | PrivateIdentifier , propType: Type, constructor: ConstructorDeclaration) {
3244032445 const reference = createPropertyAccess(createThis(), propName);
3244132446 reference.expression.parent = reference;
3244232447 reference.parent = constructor;
@@ -35972,30 +35977,32 @@ namespace ts {
3597235977 throw Debug.assertNever(prop, "Unexpected syntax kind:" + (<Node>prop).kind);
3597335978 }
3597435979
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+ }
3597935985
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);
3598735989 }
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+ }
3599136001 }
3599236002 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 );
3599436004 }
3599536005 }
35996- else {
35997- return grammarErrorOnNode(name, Diagnostics.An_object_literal_cannot_have_property_and_accessor_with_the_same_name);
35998- }
3599936006 }
3600036007 }
3600136008 }
0 commit comments