Fix lookup of optional methods in declaration emit (#32094)

This commit is contained in:
Wesley Wigham 2019-07-10 16:39:07 -07:00 committed by GitHub
parent 5289f2ede9
commit daf0a73346
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 89 additions and 13 deletions

View File

@ -4093,7 +4093,7 @@ namespace ts {
context.enclosingDeclaration = saveEnclosingDeclaration;
const optionalToken = propertySymbol.flags & SymbolFlags.Optional ? createToken(SyntaxKind.QuestionToken) : undefined;
if (propertySymbol.flags & (SymbolFlags.Function | SymbolFlags.Method) && !getPropertiesOfObjectType(propertyType).length && !isReadonlySymbol(propertySymbol)) {
const signatures = getSignaturesOfType(propertyType, SignatureKind.Call);
const signatures = getSignaturesOfType(filterType(propertyType, t => !(t.flags & TypeFlags.Undefined)), SignatureKind.Call);
for (const signature of signatures) {
const methodDeclaration = <MethodSignature>signatureToSignatureDeclarationHelper(signature, SyntaxKind.MethodSignature, context);
methodDeclaration.name = propertyName;

View File

@ -0,0 +1,23 @@
//// [declarationEmitOptionalMethod.ts]
export const Foo = (opts: {
a?(): void,
b?: () => void,
}): {
c?(): void,
d?: () => void,
} => ({ });
//// [declarationEmitOptionalMethod.js]
"use strict";
exports.__esModule = true;
exports.Foo = function (opts) { return ({}); };
//// [declarationEmitOptionalMethod.d.ts]
export declare const Foo: (opts: {
a?(): void;
b?: (() => void) | undefined;
}) => {
c?(): void;
d?: (() => void) | undefined;
};

View File

@ -0,0 +1,19 @@
=== tests/cases/compiler/declarationEmitOptionalMethod.ts ===
export const Foo = (opts: {
>Foo : Symbol(Foo, Decl(declarationEmitOptionalMethod.ts, 0, 12))
>opts : Symbol(opts, Decl(declarationEmitOptionalMethod.ts, 0, 20))
a?(): void,
>a : Symbol(a, Decl(declarationEmitOptionalMethod.ts, 0, 27))
b?: () => void,
>b : Symbol(b, Decl(declarationEmitOptionalMethod.ts, 1, 15))
}): {
c?(): void,
>c : Symbol(c, Decl(declarationEmitOptionalMethod.ts, 3, 5))
d?: () => void,
>d : Symbol(d, Decl(declarationEmitOptionalMethod.ts, 4, 15))
} => ({ });

View File

@ -0,0 +1,23 @@
=== tests/cases/compiler/declarationEmitOptionalMethod.ts ===
export const Foo = (opts: {
>Foo : (opts: { a?(): void; b?: (() => void) | undefined; }) => { c?(): void; d?: (() => void) | undefined; }
>(opts: { a?(): void, b?: () => void,}): { c?(): void, d?: () => void,} => ({ }) : (opts: { a?(): void; b?: (() => void) | undefined; }) => { c?(): void; d?: (() => void) | undefined; }
>opts : { a?(): void; b?: (() => void) | undefined; }
a?(): void,
>a : (() => void) | undefined
b?: () => void,
>b : (() => void) | undefined
}): {
c?(): void,
>c : (() => void) | undefined
d?: () => void,
>d : (() => void) | undefined
} => ({ });
>({ }) : {}
>{ } : {}

View File

@ -20,4 +20,6 @@ declare const a: string | undefined;
declare const foo: {
a: string;
};
declare const bar: {};
declare const bar: {
a?(): void;
};

View File

@ -9,8 +9,8 @@ const foo = { a! }
>a : string
const bar = {
>bar : {}
>{ a ? () { }} : {}
>bar : { a?(): void; }
>{ a ? () { }} : { a?(): void; }
a ? () { }
>a : (() => void) | undefined

View File

@ -52,15 +52,15 @@ function stringify3(anything: unknown | undefined): string { // should simplify
}
function stringify4(anything: { toString?(): string } | undefined): string {
>stringify4 : (anything: {} | undefined) => string
>anything : {} | undefined
>stringify4 : (anything: { toString?(): string; } | undefined) => string
>anything : { toString?(): string; } | undefined
>toString : (() => string) | undefined
return typeof anything === "string" ? anything.toUpperCase() : "";
>typeof anything === "string" ? anything.toUpperCase() : "" : string
>typeof anything === "string" : boolean
>typeof anything : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>anything : {} | undefined
>anything : { toString?(): string; } | undefined
>"string" : "string"
>anything.toUpperCase() : string
>anything.toUpperCase : () => string

View File

@ -1,7 +1,7 @@
=== tests/cases/conformance/salsa/bug25926.js ===
/** @type {{ a(): void; b?(n: number): number; }} */
const o1 = {
>o1 : { a(): void; }
>o1 : { a(): void; b?(n: number): number; }
>{ a() { this.b = n => n; }} : { a(): void; }
a() {
@ -10,7 +10,7 @@ const o1 = {
this.b = n => n;
>this.b = n => n : (n: number) => number
>this.b : ((n: number) => number) | undefined
>this : { a(): void; }
>this : { a(): void; b?(n: number): number; }
>b : ((n: number) => number) | undefined
>n => n : (n: number) => number
>n : number
@ -20,7 +20,7 @@ const o1 = {
/** @type {{ d(): void; e?(n: number): number; f?(n: number): number; g?: number }} */
const o2 = {
>o2 : { d(): void; g?: number | undefined; }
>o2 : { d(): void; e?(n: number): number; f?(n: number): number; g?: number | undefined; }
>{ d() { this.e = this.f = m => this.g || m; }} : { d(): void; }
d() {
@ -29,17 +29,17 @@ const o2 = {
this.e = this.f = m => this.g || m;
>this.e = this.f = m => this.g || m : (m: number) => number
>this.e : ((n: number) => number) | undefined
>this : { d(): void; g?: number | undefined; }
>this : { d(): void; e?(n: number): number; f?(n: number): number; g?: number | undefined; }
>e : ((n: number) => number) | undefined
>this.f = m => this.g || m : (m: number) => number
>this.f : ((n: number) => number) | undefined
>this : { d(): void; g?: number | undefined; }
>this : { d(): void; e?(n: number): number; f?(n: number): number; g?: number | undefined; }
>f : ((n: number) => number) | undefined
>m => this.g || m : (m: number) => number
>m : number
>this.g || m : number
>this.g : number | undefined
>this : { d(): void; g?: number | undefined; }
>this : { d(): void; e?(n: number): number; f?(n: number): number; g?: number | undefined; }
>g : number | undefined
>m : number
}

View File

@ -0,0 +1,9 @@
// @declaration: true
// @strictNullChecks: true
export const Foo = (opts: {
a?(): void,
b?: () => void,
}): {
c?(): void,
d?: () => void,
} => ({ });