Fixed expando functions with nullish properties (#54728)

This commit is contained in:
Mateusz Burzyński 2023-07-20 16:55:35 +02:00 committed by GitHub
parent 97ef321fa0
commit ec48ce89da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 264 additions and 1 deletions

View File

@ -10750,7 +10750,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}
const widened = getWidenedType(addOptionality(type, /*isProperty*/ false, definedInMethod && !definedInConstructor));
if (symbol.valueDeclaration && filterType(widened, t => !!(t.flags & ~TypeFlags.Nullable)) === neverType) {
if (symbol.valueDeclaration && isInJSFile(symbol.valueDeclaration) && filterType(widened, t => !!(t.flags & ~TypeFlags.Nullable)) === neverType) {
reportImplicitAny(symbol.valueDeclaration, anyType);
return anyType;
}

View File

@ -0,0 +1,75 @@
//// [tests/cases/compiler/expandoFunctionNullishProperty.ts] ////
//// [expandoFunctionNullishProperty.ts]
// mentioned in https://github.com/microsoft/TypeScript/issues/54220
interface TestNull {
(): void;
readonly prop: null;
}
export function testNull(): TestNull {
function inner() {}
inner.prop = null;
return inner;
}
interface TestNull2 {
(): void;
prop: string | null;
}
export function testNull2(): TestNull2 {
function inner() {}
inner.prop = null;
return inner;
}
interface TestUndefined {
(): void;
readonly prop: undefined;
}
export function testUndefined(): TestUndefined {
function inner() {}
inner.prop = undefined;
return inner;
}
//// [expandoFunctionNullishProperty.js]
// mentioned in https://github.com/microsoft/TypeScript/issues/54220
export function testNull() {
function inner() { }
inner.prop = null;
return inner;
}
export function testNull2() {
function inner() { }
inner.prop = null;
return inner;
}
export function testUndefined() {
function inner() { }
inner.prop = undefined;
return inner;
}
//// [expandoFunctionNullishProperty.d.ts]
interface TestNull {
(): void;
readonly prop: null;
}
export declare function testNull(): TestNull;
interface TestNull2 {
(): void;
prop: string | null;
}
export declare function testNull2(): TestNull2;
interface TestUndefined {
(): void;
readonly prop: undefined;
}
export declare function testUndefined(): TestUndefined;
export {};

View File

@ -0,0 +1,78 @@
//// [tests/cases/compiler/expandoFunctionNullishProperty.ts] ////
=== expandoFunctionNullishProperty.ts ===
// mentioned in https://github.com/microsoft/TypeScript/issues/54220
interface TestNull {
>TestNull : Symbol(TestNull, Decl(expandoFunctionNullishProperty.ts, 0, 0))
(): void;
readonly prop: null;
>prop : Symbol(TestNull.prop, Decl(expandoFunctionNullishProperty.ts, 3, 11))
}
export function testNull(): TestNull {
>testNull : Symbol(testNull, Decl(expandoFunctionNullishProperty.ts, 5, 1))
>TestNull : Symbol(TestNull, Decl(expandoFunctionNullishProperty.ts, 0, 0))
function inner() {}
>inner : Symbol(inner, Decl(expandoFunctionNullishProperty.ts, 7, 38))
inner.prop = null;
>inner.prop : Symbol(inner.prop, Decl(expandoFunctionNullishProperty.ts, 8, 21))
>inner : Symbol(inner, Decl(expandoFunctionNullishProperty.ts, 7, 38))
>prop : Symbol(inner.prop, Decl(expandoFunctionNullishProperty.ts, 8, 21))
return inner;
>inner : Symbol(inner, Decl(expandoFunctionNullishProperty.ts, 7, 38))
}
interface TestNull2 {
>TestNull2 : Symbol(TestNull2, Decl(expandoFunctionNullishProperty.ts, 11, 1))
(): void;
prop: string | null;
>prop : Symbol(TestNull2.prop, Decl(expandoFunctionNullishProperty.ts, 14, 11))
}
export function testNull2(): TestNull2 {
>testNull2 : Symbol(testNull2, Decl(expandoFunctionNullishProperty.ts, 16, 1))
>TestNull2 : Symbol(TestNull2, Decl(expandoFunctionNullishProperty.ts, 11, 1))
function inner() {}
>inner : Symbol(inner, Decl(expandoFunctionNullishProperty.ts, 18, 40))
inner.prop = null;
>inner.prop : Symbol(inner.prop, Decl(expandoFunctionNullishProperty.ts, 19, 21))
>inner : Symbol(inner, Decl(expandoFunctionNullishProperty.ts, 18, 40))
>prop : Symbol(inner.prop, Decl(expandoFunctionNullishProperty.ts, 19, 21))
return inner;
>inner : Symbol(inner, Decl(expandoFunctionNullishProperty.ts, 18, 40))
}
interface TestUndefined {
>TestUndefined : Symbol(TestUndefined, Decl(expandoFunctionNullishProperty.ts, 22, 1))
(): void;
readonly prop: undefined;
>prop : Symbol(TestUndefined.prop, Decl(expandoFunctionNullishProperty.ts, 25, 11))
}
export function testUndefined(): TestUndefined {
>testUndefined : Symbol(testUndefined, Decl(expandoFunctionNullishProperty.ts, 27, 1))
>TestUndefined : Symbol(TestUndefined, Decl(expandoFunctionNullishProperty.ts, 22, 1))
function inner() {}
>inner : Symbol(inner, Decl(expandoFunctionNullishProperty.ts, 29, 48))
inner.prop = undefined;
>inner.prop : Symbol(inner.prop, Decl(expandoFunctionNullishProperty.ts, 30, 21))
>inner : Symbol(inner, Decl(expandoFunctionNullishProperty.ts, 29, 48))
>prop : Symbol(inner.prop, Decl(expandoFunctionNullishProperty.ts, 30, 21))
>undefined : Symbol(undefined)
return inner;
>inner : Symbol(inner, Decl(expandoFunctionNullishProperty.ts, 29, 48))
}

View File

@ -0,0 +1,72 @@
//// [tests/cases/compiler/expandoFunctionNullishProperty.ts] ////
=== expandoFunctionNullishProperty.ts ===
// mentioned in https://github.com/microsoft/TypeScript/issues/54220
interface TestNull {
(): void;
readonly prop: null;
>prop : null
}
export function testNull(): TestNull {
>testNull : () => TestNull
function inner() {}
>inner : { (): void; prop: null; }
inner.prop = null;
>inner.prop = null : null
>inner.prop : null
>inner : { (): void; prop: null; }
>prop : null
return inner;
>inner : { (): void; prop: null; }
}
interface TestNull2 {
(): void;
prop: string | null;
>prop : string | null
}
export function testNull2(): TestNull2 {
>testNull2 : () => TestNull2
function inner() {}
>inner : { (): void; prop: null; }
inner.prop = null;
>inner.prop = null : null
>inner.prop : null
>inner : { (): void; prop: null; }
>prop : null
return inner;
>inner : { (): void; prop: null; }
}
interface TestUndefined {
(): void;
readonly prop: undefined;
>prop : undefined
}
export function testUndefined(): TestUndefined {
>testUndefined : () => TestUndefined
function inner() {}
>inner : { (): void; prop: undefined; }
inner.prop = undefined;
>inner.prop = undefined : undefined
>inner.prop : undefined
>inner : { (): void; prop: undefined; }
>prop : undefined
>undefined : undefined
return inner;
>inner : { (): void; prop: undefined; }
}

View File

@ -0,0 +1,38 @@
// @strict: true
// @target: esnext
// @declaration: true
// mentioned in https://github.com/microsoft/TypeScript/issues/54220
interface TestNull {
(): void;
readonly prop: null;
}
export function testNull(): TestNull {
function inner() {}
inner.prop = null;
return inner;
}
interface TestNull2 {
(): void;
prop: string | null;
}
export function testNull2(): TestNull2 {
function inner() {}
inner.prop = null;
return inner;
}
interface TestUndefined {
(): void;
readonly prop: undefined;
}
export function testUndefined(): TestUndefined {
function inner() {}
inner.prop = undefined;
return inner;
}