Skip to content

Commit ce3dbef

Browse files
authored
Support properties of mapped types in assertion control flow analysis (#40482)
* Support properties of mapped types in assertion control flow analysis * Add regression test * Accept new baselines
1 parent 8cd4793 commit ce3dbef

File tree

6 files changed

+119
-0
lines changed

6 files changed

+119
-0
lines changed

src/compiler/checker.ts

+6
Original file line numberDiff line numberDiff line change
@@ -21103,6 +21103,12 @@ namespace ts {
2110321103
return getTypeOfSymbol(symbol);
2110421104
}
2110521105
if (symbol.flags & (SymbolFlags.Variable | SymbolFlags.Property)) {
21106+
if (getCheckFlags(symbol) & CheckFlags.Mapped) {
21107+
const origin = (<MappedSymbol>symbol).syntheticOrigin;
21108+
if (origin && getExplicitTypeOfSymbol(origin)) {
21109+
return getTypeOfSymbol(symbol);
21110+
}
21111+
}
2110621112
const declaration = symbol.valueDeclaration;
2110721113
if (declaration) {
2110821114
if (isDeclarationWithExplicitTypeAnnotation(declaration)) {

tests/baselines/reference/neverReturningFunctions1.errors.txt

+14
Original file line numberDiff line numberDiff line change
@@ -318,4 +318,18 @@ tests/cases/conformance/controlFlow/neverReturningFunctions1.ts(153,5): error TS
318318
this.throw()
319319
}
320320
}
321+
322+
// Repro from #40346
323+
324+
interface Services {
325+
panic(message: string): never;
326+
}
327+
328+
function foo(services: Readonly<Services>, s: string | null): string {
329+
if (s === null) {
330+
services.panic("ouch");
331+
} else {
332+
return s;
333+
}
334+
}
321335

tests/baselines/reference/neverReturningFunctions1.js

+22
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,20 @@ class SuperThrowable extends MyThrowable {
247247
this.throw()
248248
}
249249
}
250+
251+
// Repro from #40346
252+
253+
interface Services {
254+
panic(message: string): never;
255+
}
256+
257+
function foo(services: Readonly<Services>, s: string | null): string {
258+
if (s === null) {
259+
services.panic("ouch");
260+
} else {
261+
return s;
262+
}
263+
}
250264

251265

252266
//// [neverReturningFunctions1.js]
@@ -467,6 +481,14 @@ var SuperThrowable = /** @class */ (function (_super) {
467481
};
468482
return SuperThrowable;
469483
}(MyThrowable));
484+
function foo(services, s) {
485+
if (s === null) {
486+
services.panic("ouch");
487+
}
488+
else {
489+
return s;
490+
}
491+
}
470492

471493

472494
//// [neverReturningFunctions1.d.ts]

tests/baselines/reference/neverReturningFunctions1.symbols

+31
Original file line numberDiff line numberDiff line change
@@ -635,3 +635,34 @@ class SuperThrowable extends MyThrowable {
635635
}
636636
}
637637

638+
// Repro from #40346
639+
640+
interface Services {
641+
>Services : Symbol(Services, Decl(neverReturningFunctions1.ts, 247, 1))
642+
643+
panic(message: string): never;
644+
>panic : Symbol(Services.panic, Decl(neverReturningFunctions1.ts, 251, 20))
645+
>message : Symbol(message, Decl(neverReturningFunctions1.ts, 252, 10))
646+
}
647+
648+
function foo(services: Readonly<Services>, s: string | null): string {
649+
>foo : Symbol(foo, Decl(neverReturningFunctions1.ts, 253, 1))
650+
>services : Symbol(services, Decl(neverReturningFunctions1.ts, 255, 13))
651+
>Readonly : Symbol(Readonly, Decl(lib.es5.d.ts, --, --))
652+
>Services : Symbol(Services, Decl(neverReturningFunctions1.ts, 247, 1))
653+
>s : Symbol(s, Decl(neverReturningFunctions1.ts, 255, 42))
654+
655+
if (s === null) {
656+
>s : Symbol(s, Decl(neverReturningFunctions1.ts, 255, 42))
657+
658+
services.panic("ouch");
659+
>services.panic : Symbol(panic, Decl(neverReturningFunctions1.ts, 251, 20))
660+
>services : Symbol(services, Decl(neverReturningFunctions1.ts, 255, 13))
661+
>panic : Symbol(panic, Decl(neverReturningFunctions1.ts, 251, 20))
662+
663+
} else {
664+
return s;
665+
>s : Symbol(s, Decl(neverReturningFunctions1.ts, 255, 42))
666+
}
667+
}
668+

tests/baselines/reference/neverReturningFunctions1.types

+32
Original file line numberDiff line numberDiff line change
@@ -706,3 +706,35 @@ class SuperThrowable extends MyThrowable {
706706
}
707707
}
708708

709+
// Repro from #40346
710+
711+
interface Services {
712+
panic(message: string): never;
713+
>panic : (message: string) => never
714+
>message : string
715+
}
716+
717+
function foo(services: Readonly<Services>, s: string | null): string {
718+
>foo : (services: Readonly<Services>, s: string | null) => string
719+
>services : Readonly<Services>
720+
>s : string | null
721+
>null : null
722+
723+
if (s === null) {
724+
>s === null : boolean
725+
>s : string | null
726+
>null : null
727+
728+
services.panic("ouch");
729+
>services.panic("ouch") : never
730+
>services.panic : (message: string) => never
731+
>services : Readonly<Services>
732+
>panic : (message: string) => never
733+
>"ouch" : "ouch"
734+
735+
} else {
736+
return s;
737+
>s : string
738+
}
739+
}
740+

tests/cases/conformance/controlFlow/neverReturningFunctions1.ts

+14
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,17 @@ class SuperThrowable extends MyThrowable {
250250
this.throw()
251251
}
252252
}
253+
254+
// Repro from #40346
255+
256+
interface Services {
257+
panic(message: string): never;
258+
}
259+
260+
function foo(services: Readonly<Services>, s: string | null): string {
261+
if (s === null) {
262+
services.panic("ouch");
263+
} else {
264+
return s;
265+
}
266+
}

0 commit comments

Comments
 (0)