Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Add regression test
  • Loading branch information
ahejlsberg committed Aug 17, 2016
commit b93cdecdf5165ad0b569ca9a6b9d54626b4855b6
63 changes: 63 additions & 0 deletions tests/baselines/reference/nestedLoopTypeGuards.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//// [nestedLoopTypeGuards.ts]
// Repros from #10378

function f1() {
var a: boolean | number | string;
if (typeof a !== 'boolean') {
// a is narrowed to "number | string"
for (var i = 0; i < 1; i++) {
for (var j = 0; j < 1; j++) {}
if (typeof a === 'string') {
// a is narrowed to "string'
for (var j = 0; j < 1; j++) {
a.length; // Should not error here
}
}
}
}
}

function f2() {
var a: string | number;
if (typeof a === 'string') {
while (1) {
while (1) {}
if (typeof a === 'string') {
while (1) {
a.length; // Should not error here
}
}
}
}
}

//// [nestedLoopTypeGuards.js]
// Repros from #10378
function f1() {
var a;
if (typeof a !== 'boolean') {
// a is narrowed to "number | string"
for (var i = 0; i < 1; i++) {
for (var j = 0; j < 1; j++) { }
if (typeof a === 'string') {
// a is narrowed to "string'
for (var j = 0; j < 1; j++) {
a.length; // Should not error here
}
}
}
}
}
function f2() {
var a;
if (typeof a === 'string') {
while (1) {
while (1) { }
if (typeof a === 'string') {
while (1) {
a.length; // Should not error here
}
}
}
}
}
66 changes: 66 additions & 0 deletions tests/baselines/reference/nestedLoopTypeGuards.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
=== tests/cases/compiler/nestedLoopTypeGuards.ts ===
// Repros from #10378

function f1() {
>f1 : Symbol(f1, Decl(nestedLoopTypeGuards.ts, 0, 0))

var a: boolean | number | string;
>a : Symbol(a, Decl(nestedLoopTypeGuards.ts, 3, 7))

if (typeof a !== 'boolean') {
>a : Symbol(a, Decl(nestedLoopTypeGuards.ts, 3, 7))

// a is narrowed to "number | string"
for (var i = 0; i < 1; i++) {
>i : Symbol(i, Decl(nestedLoopTypeGuards.ts, 6, 16))
>i : Symbol(i, Decl(nestedLoopTypeGuards.ts, 6, 16))
>i : Symbol(i, Decl(nestedLoopTypeGuards.ts, 6, 16))

for (var j = 0; j < 1; j++) {}
>j : Symbol(j, Decl(nestedLoopTypeGuards.ts, 7, 20), Decl(nestedLoopTypeGuards.ts, 10, 24))
>j : Symbol(j, Decl(nestedLoopTypeGuards.ts, 7, 20), Decl(nestedLoopTypeGuards.ts, 10, 24))
>j : Symbol(j, Decl(nestedLoopTypeGuards.ts, 7, 20), Decl(nestedLoopTypeGuards.ts, 10, 24))

if (typeof a === 'string') {
>a : Symbol(a, Decl(nestedLoopTypeGuards.ts, 3, 7))

// a is narrowed to "string'
for (var j = 0; j < 1; j++) {
>j : Symbol(j, Decl(nestedLoopTypeGuards.ts, 7, 20), Decl(nestedLoopTypeGuards.ts, 10, 24))
>j : Symbol(j, Decl(nestedLoopTypeGuards.ts, 7, 20), Decl(nestedLoopTypeGuards.ts, 10, 24))
>j : Symbol(j, Decl(nestedLoopTypeGuards.ts, 7, 20), Decl(nestedLoopTypeGuards.ts, 10, 24))

a.length; // Should not error here
>a.length : Symbol(String.length, Decl(lib.d.ts, --, --))
>a : Symbol(a, Decl(nestedLoopTypeGuards.ts, 3, 7))
>length : Symbol(String.length, Decl(lib.d.ts, --, --))
}
}
}
}
}

function f2() {
>f2 : Symbol(f2, Decl(nestedLoopTypeGuards.ts, 16, 1))

var a: string | number;
>a : Symbol(a, Decl(nestedLoopTypeGuards.ts, 19, 7))

if (typeof a === 'string') {
>a : Symbol(a, Decl(nestedLoopTypeGuards.ts, 19, 7))

while (1) {
while (1) {}
if (typeof a === 'string') {
>a : Symbol(a, Decl(nestedLoopTypeGuards.ts, 19, 7))

while (1) {
a.length; // Should not error here
>a.length : Symbol(String.length, Decl(lib.d.ts, --, --))
>a : Symbol(a, Decl(nestedLoopTypeGuards.ts, 19, 7))
>length : Symbol(String.length, Decl(lib.d.ts, --, --))
}
}
}
}
}
96 changes: 96 additions & 0 deletions tests/baselines/reference/nestedLoopTypeGuards.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
=== tests/cases/compiler/nestedLoopTypeGuards.ts ===
// Repros from #10378

function f1() {
>f1 : () => void

var a: boolean | number | string;
>a : string | number | boolean

if (typeof a !== 'boolean') {
>typeof a !== 'boolean' : boolean
>typeof a : string
>a : string | number | boolean
>'boolean' : "boolean"

// a is narrowed to "number | string"
for (var i = 0; i < 1; i++) {
>i : number
>0 : number
>i < 1 : boolean
>i : number
>1 : number
>i++ : number
>i : number

for (var j = 0; j < 1; j++) {}
>j : number
>0 : number
>j < 1 : boolean
>j : number
>1 : number
>j++ : number
>j : number

if (typeof a === 'string') {
>typeof a === 'string' : boolean
>typeof a : string
>a : string | number
>'string' : "string"

// a is narrowed to "string'
for (var j = 0; j < 1; j++) {
>j : number
>0 : number
>j < 1 : boolean
>j : number
>1 : number
>j++ : number
>j : number

a.length; // Should not error here
>a.length : number
>a : string
>length : number
}
}
}
}
}

function f2() {
>f2 : () => void

var a: string | number;
>a : string | number

if (typeof a === 'string') {
>typeof a === 'string' : boolean
>typeof a : string
>a : string | number
>'string' : "string"

while (1) {
>1 : number

while (1) {}
>1 : number

if (typeof a === 'string') {
>typeof a === 'string' : boolean
>typeof a : string
>a : string
>'string' : "string"

while (1) {
>1 : number

a.length; // Should not error here
>a.length : number
>a : string
>length : number
}
}
}
}
}
31 changes: 31 additions & 0 deletions tests/cases/compiler/nestedLoopTypeGuards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Repros from #10378

function f1() {
var a: boolean | number | string;
if (typeof a !== 'boolean') {
// a is narrowed to "number | string"
for (var i = 0; i < 1; i++) {
for (var j = 0; j < 1; j++) {}
if (typeof a === 'string') {
// a is narrowed to "string'
for (var j = 0; j < 1; j++) {
a.length; // Should not error here
}
}
}
}
}

function f2() {
var a: string | number;
if (typeof a === 'string') {
while (1) {
while (1) {}
if (typeof a === 'string') {
while (1) {
a.length; // Should not error here
}
}
}
}
}