mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-04 21:53:42 -06:00
Fixed declaration emit for undefined properties inferred from functions in other array elements (#53938)
This commit is contained in:
parent
2beeb8b931
commit
1518cd98f4
@ -7126,36 +7126,37 @@ 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;
|
||||
}
|
||||
}
|
||||
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)) {
|
||||
const d = propertySymbol.declarations?.find(d => d.kind === SyntaxKind.JSDocPropertyTag)! as JSDocPropertyTag;
|
||||
|
||||
@ -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;
|
||||
})[];
|
||||
@ -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))
|
||||
|
||||
@ -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
|
||||
|
||||
@ -0,0 +1,5 @@
|
||||
// @declaration: true
|
||||
|
||||
// repro from https://github.com/microsoft/TypeScript/issues/53914
|
||||
|
||||
export let b = [{ foo: 0, m() {} }, { bar: 1 }];
|
||||
Loading…
x
Reference in New Issue
Block a user