Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7154,35 +7154,36 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const methodDeclaration = signatureToSignatureDeclarationHelper(signature, SyntaxKind.MethodSignature, context, { name: propertyName, questionToken: optionalToken }) as MethodSignature;
typeElements.push(preserveCommentsOn(methodDeclaration));
}
if (signatures.length || !optionalToken) {
return;
}
Comment on lines +7157 to +7159
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the fix, the rest is just dedented because now we fallthrough to the rest of the logic in this case

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
let propertyTypeNode: TypeNode;
if (shouldUsePlaceholderForProperty(propertySymbol, context)) {
propertyTypeNode = createElidedInformationPlaceholder(context);
}
else {
let propertyTypeNode: TypeNode;
if (shouldUsePlaceholderForProperty(propertySymbol, context)) {
propertyTypeNode = createElidedInformationPlaceholder(context);
if (propertyIsReverseMapped) {
context.reverseMappedStack ||= [];
context.reverseMappedStack.push(propertySymbol as ReverseMappedSymbol);
}
else {
if (propertyIsReverseMapped) {
context.reverseMappedStack ||= [];
context.reverseMappedStack.push(propertySymbol as ReverseMappedSymbol);
}
propertyTypeNode = propertyType ? serializeTypeForDeclaration(context, propertyType, propertySymbol, saveEnclosingDeclaration) : factory.createKeywordTypeNode(SyntaxKind.AnyKeyword);
if (propertyIsReverseMapped) {
context.reverseMappedStack!.pop();
}
propertyTypeNode = propertyType ? serializeTypeForDeclaration(context, propertyType, propertySymbol, saveEnclosingDeclaration) : factory.createKeywordTypeNode(SyntaxKind.AnyKeyword);
if (propertyIsReverseMapped) {
context.reverseMappedStack!.pop();
}
}

const modifiers = isReadonlySymbol(propertySymbol) ? [factory.createToken(SyntaxKind.ReadonlyKeyword)] : undefined;
if (modifiers) {
context.approximateLength += 9;
}
const propertySignature = factory.createPropertySignature(
modifiers,
propertyName,
optionalToken,
propertyTypeNode);

typeElements.push(preserveCommentsOn(propertySignature));
const modifiers = isReadonlySymbol(propertySymbol) ? [factory.createToken(SyntaxKind.ReadonlyKeyword)] : undefined;
if (modifiers) {
context.approximateLength += 9;
}
const propertySignature = factory.createPropertySignature(
modifiers,
propertyName,
optionalToken,
propertyTypeNode);

typeElements.push(preserveCommentsOn(propertySignature));

function preserveCommentsOn<T extends Node>(node: T) {
if (some(propertySymbol.declarations, d => d.kind === SyntaxKind.JSDocPropertyTag)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [declarationEmitInferredUndefinedPropFromFunctionInArray.ts]
// repro from https://github.com/microsoft/TypeScript/issues/53914

export let b = [{ foo: 0, m() {} }, { bar: 1 }];

//// [declarationEmitInferredUndefinedPropFromFunctionInArray.js]
"use strict";
// repro from https://github.com/microsoft/TypeScript/issues/53914
Object.defineProperty(exports, "__esModule", { value: true });
exports.b = void 0;
exports.b = [{ foo: 0, m: function () { } }, { bar: 1 }];


//// [declarationEmitInferredUndefinedPropFromFunctionInArray.d.ts]
export declare let b: ({
foo: number;
m(): void;
bar?: undefined;
} | {
bar: number;
foo?: undefined;
m?: undefined;
})[];
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
=== tests/cases/compiler/declarationEmitInferredUndefinedPropFromFunctionInArray.ts ===
// repro from https://github.com/microsoft/TypeScript/issues/53914

export let b = [{ foo: 0, m() {} }, { bar: 1 }];
>b : Symbol(b, Decl(declarationEmitInferredUndefinedPropFromFunctionInArray.ts, 2, 10))
>foo : Symbol(foo, Decl(declarationEmitInferredUndefinedPropFromFunctionInArray.ts, 2, 17))
>m : Symbol(m, Decl(declarationEmitInferredUndefinedPropFromFunctionInArray.ts, 2, 25))
>bar : Symbol(bar, Decl(declarationEmitInferredUndefinedPropFromFunctionInArray.ts, 2, 37))

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
=== tests/cases/compiler/declarationEmitInferredUndefinedPropFromFunctionInArray.ts ===
// repro from https://github.com/microsoft/TypeScript/issues/53914

export let b = [{ foo: 0, m() {} }, { bar: 1 }];
>b : ({ foo: number; m(): void; bar?: undefined; } | { bar: number; foo?: undefined; m?: undefined; })[]
>[{ foo: 0, m() {} }, { bar: 1 }] : ({ foo: number; m(): void; } | { bar: number; })[]
>{ foo: 0, m() {} } : { foo: number; m(): void; }
>foo : number
>0 : 0
>m : () => void
>{ bar: 1 } : { bar: number; }
>bar : number
>1 : 1

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @declaration: true

// repro from https://github.com/microsoft/TypeScript/issues/53914

export let b = [{ foo: 0, m() {} }, { bar: 1 }];