mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-15 03:23:08 -06:00
Add tests
This commit is contained in:
parent
144f4ffc7c
commit
a9c2b23df5
59
tests/cases/compiler/controlFlowArrayErrors.ts
Normal file
59
tests/cases/compiler/controlFlowArrayErrors.ts
Normal file
@ -0,0 +1,59 @@
|
||||
// @noImplicitAny: true
|
||||
|
||||
declare function cond(): boolean;
|
||||
|
||||
function f1() {
|
||||
let x = [];
|
||||
let y = x; // Implicit any[] error
|
||||
x.push(5);
|
||||
let z = x;
|
||||
}
|
||||
|
||||
function f2() {
|
||||
let x;
|
||||
x = [];
|
||||
let y = x; // Implicit any[] error
|
||||
x.push(5);
|
||||
let z = x;
|
||||
}
|
||||
|
||||
function f3() {
|
||||
let x = []; // Implicit any[] error in some locations
|
||||
x.push(5);
|
||||
function g() {
|
||||
x; // Implicit any[] error
|
||||
}
|
||||
}
|
||||
|
||||
function f4() {
|
||||
let x;
|
||||
x = [5, "hello"]; // Non-evolving array
|
||||
x.push(true); // Error
|
||||
}
|
||||
|
||||
function f5() {
|
||||
let x = [5, "hello"]; // Non-evolving array
|
||||
x.push(true); // Error
|
||||
}
|
||||
|
||||
function f6() {
|
||||
let x;
|
||||
if (cond()) {
|
||||
x = [];
|
||||
x.push(5);
|
||||
x.push("hello");
|
||||
}
|
||||
else {
|
||||
x = [true]; // Non-evolving array
|
||||
}
|
||||
x; // boolean[] | (string | number)[]
|
||||
x.push(99); // Error
|
||||
}
|
||||
|
||||
function f7() {
|
||||
let x = []; // x has evolving array value
|
||||
x.push(5);
|
||||
let y = x; // y has non-evolving array value
|
||||
x.push("hello"); // Ok
|
||||
y.push("hello"); // Error
|
||||
}
|
||||
141
tests/cases/compiler/controlFlowArrays.ts
Normal file
141
tests/cases/compiler/controlFlowArrays.ts
Normal file
@ -0,0 +1,141 @@
|
||||
// @strictNullChecks: true
|
||||
// @noImplicitAny: true
|
||||
|
||||
declare function cond(): boolean;
|
||||
|
||||
function f1() {
|
||||
let x = [];
|
||||
x[0] = 5;
|
||||
x[1] = "hello";
|
||||
x[2] = true;
|
||||
return x; // (string | number | boolean)[]
|
||||
}
|
||||
|
||||
function f2() {
|
||||
let x = [];
|
||||
x.push(5);
|
||||
x.push("hello");
|
||||
x.push(true);
|
||||
return x; // (string | number | boolean)[]
|
||||
}
|
||||
|
||||
function f3() {
|
||||
let x;
|
||||
x = [];
|
||||
x.push(5, "hello");
|
||||
return x; // (string | number)[]
|
||||
}
|
||||
|
||||
function f4() {
|
||||
let x = [];
|
||||
if (cond()) {
|
||||
x.push(5);
|
||||
}
|
||||
else {
|
||||
x.push("hello");
|
||||
}
|
||||
return x; // (string | number)[]
|
||||
}
|
||||
|
||||
function f5() {
|
||||
let x;
|
||||
if (cond()) {
|
||||
x = [];
|
||||
x.push(5);
|
||||
}
|
||||
else {
|
||||
x = [];
|
||||
x.push("hello");
|
||||
}
|
||||
return x; // (string | number)[]
|
||||
}
|
||||
|
||||
function f6() {
|
||||
let x;
|
||||
if (cond()) {
|
||||
x = 5;
|
||||
}
|
||||
else {
|
||||
x = [];
|
||||
x.push("hello");
|
||||
}
|
||||
return x; // number | string[]
|
||||
}
|
||||
|
||||
function f7() {
|
||||
let x = null;
|
||||
if (cond()) {
|
||||
x = [];
|
||||
while (cond()) {
|
||||
x.push("hello");
|
||||
}
|
||||
}
|
||||
return x; // string[] | null
|
||||
}
|
||||
|
||||
function f8() {
|
||||
let x = [];
|
||||
x.push(5);
|
||||
if (cond()) return x; // number[]
|
||||
x.push("hello");
|
||||
if (cond()) return x; // (string | number)[]
|
||||
x.push(true);
|
||||
return x; // (string | number | boolean)[]
|
||||
}
|
||||
|
||||
function f9() {
|
||||
let x = [];
|
||||
if (cond()) {
|
||||
x.push(5);
|
||||
return x; // number[]
|
||||
}
|
||||
else {
|
||||
x.push("hello");
|
||||
return x; // string[]
|
||||
}
|
||||
}
|
||||
|
||||
function f10() {
|
||||
let x = [];
|
||||
if (cond()) {
|
||||
x.push(true);
|
||||
x; // boolean[]
|
||||
}
|
||||
else {
|
||||
x.push(5);
|
||||
x; // number[]
|
||||
while (cond()) {
|
||||
x.push("hello");
|
||||
}
|
||||
x; // (string | number)[]
|
||||
}
|
||||
x.push(99);
|
||||
return x; // (string | number | boolean)[]
|
||||
}
|
||||
|
||||
function f11() {
|
||||
let x = [];
|
||||
return x; // never[]
|
||||
}
|
||||
|
||||
function f12() {
|
||||
let x;
|
||||
x = [];
|
||||
return x; // never[]
|
||||
}
|
||||
|
||||
function f13() {
|
||||
var x = [];
|
||||
x.push(5);
|
||||
x.push("hello");
|
||||
x.push(true);
|
||||
return x; // (string | number | boolean)[]
|
||||
}
|
||||
|
||||
function f14() {
|
||||
const x = [];
|
||||
x.push(5);
|
||||
x.push("hello");
|
||||
x.push(true);
|
||||
return x; // (string | number | boolean)[]
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user