Skip to content

🤖 Pick PR #38398 (Fix crash caused by assertion with ...) into release-3.9 #38400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20280,7 +20280,7 @@ namespace ts {
const predicate = getTypePredicateOfSignature(signature);
if (predicate && (predicate.kind === TypePredicateKind.AssertsThis || predicate.kind === TypePredicateKind.AssertsIdentifier)) {
const flowType = getTypeAtFlowNode(flow.antecedent);
const type = getTypeFromFlowType(flowType);
const type = finalizeEvolvingArrayType(getTypeFromFlowType(flowType));
const narrowedType = predicate.type ? narrowTypeByTypePredicate(type, predicate, flow.node, /*assumeTrue*/ true) :
predicate.kind === TypePredicateKind.AssertsIdentifier && predicate.parameterIndex >= 0 && predicate.parameterIndex < flow.node.arguments.length ? narrowTypeByAssertion(type, flow.node.arguments[predicate.parameterIndex]) :
type;
Expand Down
23 changes: 23 additions & 0 deletions tests/baselines/reference/evolvingArrayTypeInAssert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [evolvingArrayTypeInAssert.ts]
export function unsafeCast<T>(_value: unknown): asserts _value is T { }

function yadda() {
let out = [];
out.push(100)
unsafeCast<any>(out);
return out;
}


//// [evolvingArrayTypeInAssert.js]
"use strict";
exports.__esModule = true;
exports.unsafeCast = void 0;
function unsafeCast(_value) { }
exports.unsafeCast = unsafeCast;
function yadda() {
var out = [];
out.push(100);
unsafeCast(out);
return out;
}
27 changes: 27 additions & 0 deletions tests/baselines/reference/evolvingArrayTypeInAssert.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
=== tests/cases/compiler/evolvingArrayTypeInAssert.ts ===
export function unsafeCast<T>(_value: unknown): asserts _value is T { }
>unsafeCast : Symbol(unsafeCast, Decl(evolvingArrayTypeInAssert.ts, 0, 0))
>T : Symbol(T, Decl(evolvingArrayTypeInAssert.ts, 0, 27))
>_value : Symbol(_value, Decl(evolvingArrayTypeInAssert.ts, 0, 30))
>_value : Symbol(_value, Decl(evolvingArrayTypeInAssert.ts, 0, 30))
>T : Symbol(T, Decl(evolvingArrayTypeInAssert.ts, 0, 27))

function yadda() {
>yadda : Symbol(yadda, Decl(evolvingArrayTypeInAssert.ts, 0, 71))

let out = [];
>out : Symbol(out, Decl(evolvingArrayTypeInAssert.ts, 3, 7))

out.push(100)
>out.push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --))
>out : Symbol(out, Decl(evolvingArrayTypeInAssert.ts, 3, 7))
>push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --))

unsafeCast<any>(out);
>unsafeCast : Symbol(unsafeCast, Decl(evolvingArrayTypeInAssert.ts, 0, 0))
>out : Symbol(out, Decl(evolvingArrayTypeInAssert.ts, 3, 7))

return out;
>out : Symbol(out, Decl(evolvingArrayTypeInAssert.ts, 3, 7))
}

28 changes: 28 additions & 0 deletions tests/baselines/reference/evolvingArrayTypeInAssert.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
=== tests/cases/compiler/evolvingArrayTypeInAssert.ts ===
export function unsafeCast<T>(_value: unknown): asserts _value is T { }
>unsafeCast : <T>(_value: unknown) => asserts _value is T
>_value : unknown

function yadda() {
>yadda : () => number[]

let out = [];
>out : any[]
>[] : never[]

out.push(100)
>out.push(100) : number
>out.push : (...items: any[]) => number
>out : any[]
>push : (...items: any[]) => number
>100 : 100

unsafeCast<any>(out);
>unsafeCast<any>(out) : void
>unsafeCast : <T>(_value: unknown) => asserts _value is T
>out : number[]

return out;
>out : number[]
}

10 changes: 10 additions & 0 deletions tests/cases/compiler/evolvingArrayTypeInAssert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @strict: true

export function unsafeCast<T>(_value: unknown): asserts _value is T { }

function yadda() {
let out = [];
out.push(100)
unsafeCast<any>(out);
return out;
}