Skip to content

Commit 7d5b207

Browse files
ahejlsbergtypescript-bot
authored andcommitted
Cherry-pick PR microsoft#38398 into release-3.9
Component commits: 99c5c09 Properly finalize evolving array type in getTypeAtFlowCall b355cd4 Add regression test
1 parent e261cdd commit 7d5b207

File tree

5 files changed

+89
-1
lines changed

5 files changed

+89
-1
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20280,7 +20280,7 @@ namespace ts {
2028020280
const predicate = getTypePredicateOfSignature(signature);
2028120281
if (predicate && (predicate.kind === TypePredicateKind.AssertsThis || predicate.kind === TypePredicateKind.AssertsIdentifier)) {
2028220282
const flowType = getTypeAtFlowNode(flow.antecedent);
20283-
const type = getTypeFromFlowType(flowType);
20283+
const type = finalizeEvolvingArrayType(getTypeFromFlowType(flowType));
2028420284
const narrowedType = predicate.type ? narrowTypeByTypePredicate(type, predicate, flow.node, /*assumeTrue*/ true) :
2028520285
predicate.kind === TypePredicateKind.AssertsIdentifier && predicate.parameterIndex >= 0 && predicate.parameterIndex < flow.node.arguments.length ? narrowTypeByAssertion(type, flow.node.arguments[predicate.parameterIndex]) :
2028620286
type;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [evolvingArrayTypeInAssert.ts]
2+
export function unsafeCast<T>(_value: unknown): asserts _value is T { }
3+
4+
function yadda() {
5+
let out = [];
6+
out.push(100)
7+
unsafeCast<any>(out);
8+
return out;
9+
}
10+
11+
12+
//// [evolvingArrayTypeInAssert.js]
13+
"use strict";
14+
exports.__esModule = true;
15+
exports.unsafeCast = void 0;
16+
function unsafeCast(_value) { }
17+
exports.unsafeCast = unsafeCast;
18+
function yadda() {
19+
var out = [];
20+
out.push(100);
21+
unsafeCast(out);
22+
return out;
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
=== tests/cases/compiler/evolvingArrayTypeInAssert.ts ===
2+
export function unsafeCast<T>(_value: unknown): asserts _value is T { }
3+
>unsafeCast : Symbol(unsafeCast, Decl(evolvingArrayTypeInAssert.ts, 0, 0))
4+
>T : Symbol(T, Decl(evolvingArrayTypeInAssert.ts, 0, 27))
5+
>_value : Symbol(_value, Decl(evolvingArrayTypeInAssert.ts, 0, 30))
6+
>_value : Symbol(_value, Decl(evolvingArrayTypeInAssert.ts, 0, 30))
7+
>T : Symbol(T, Decl(evolvingArrayTypeInAssert.ts, 0, 27))
8+
9+
function yadda() {
10+
>yadda : Symbol(yadda, Decl(evolvingArrayTypeInAssert.ts, 0, 71))
11+
12+
let out = [];
13+
>out : Symbol(out, Decl(evolvingArrayTypeInAssert.ts, 3, 7))
14+
15+
out.push(100)
16+
>out.push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --))
17+
>out : Symbol(out, Decl(evolvingArrayTypeInAssert.ts, 3, 7))
18+
>push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --))
19+
20+
unsafeCast<any>(out);
21+
>unsafeCast : Symbol(unsafeCast, Decl(evolvingArrayTypeInAssert.ts, 0, 0))
22+
>out : Symbol(out, Decl(evolvingArrayTypeInAssert.ts, 3, 7))
23+
24+
return out;
25+
>out : Symbol(out, Decl(evolvingArrayTypeInAssert.ts, 3, 7))
26+
}
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
=== tests/cases/compiler/evolvingArrayTypeInAssert.ts ===
2+
export function unsafeCast<T>(_value: unknown): asserts _value is T { }
3+
>unsafeCast : <T>(_value: unknown) => asserts _value is T
4+
>_value : unknown
5+
6+
function yadda() {
7+
>yadda : () => number[]
8+
9+
let out = [];
10+
>out : any[]
11+
>[] : never[]
12+
13+
out.push(100)
14+
>out.push(100) : number
15+
>out.push : (...items: any[]) => number
16+
>out : any[]
17+
>push : (...items: any[]) => number
18+
>100 : 100
19+
20+
unsafeCast<any>(out);
21+
>unsafeCast<any>(out) : void
22+
>unsafeCast : <T>(_value: unknown) => asserts _value is T
23+
>out : number[]
24+
25+
return out;
26+
>out : number[]
27+
}
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @strict: true
2+
3+
export function unsafeCast<T>(_value: unknown): asserts _value is T { }
4+
5+
function yadda() {
6+
let out = [];
7+
out.push(100)
8+
unsafeCast<any>(out);
9+
return out;
10+
}

0 commit comments

Comments
 (0)