TypeScript/tests/cases/conformance/expressions/assignmentOperator/assignmentTypeNarrowing.ts
Matt McCutchen cc1c2ab6b2 Go back to the old narrowing algorithm (pre #26143) and avoid #26130 by
skipping narrowing if the old algorithm produces a type to which the
assigned type is not assignable.

This also means we'll no longer narrow for erroneous assignments where
the assigned type is not assignable to the declared type.  This is the
reason for the numericLiteralTypes3 baseline change.

Fixes #26405.
2018-08-17 21:35:03 -04:00

35 lines
483 B
TypeScript

let x: string | number | boolean | RegExp;
x = "";
x; // string
[x] = [true];
x; // boolean
[x = ""] = [1];
x; // string | number
({x} = {x: true});
x; // boolean
({y: x} = {y: 1});
x; // number
({x = ""} = {x: true});
x; // string | boolean
({y: x = /a/} = {y: 1});
x; // number | RegExp
let a: string[];
for (x of a) {
x; // string
}
// Repro from #26405
type AOrArrA<T> = T | T[];
const arr: AOrArrA<{x?: "ok"}> = [{ x: "ok" }]; // weak type
arr.push({ x: "ok" });