TypeScript/tests/cases/compiler/controlFlowNoImplicitAny.ts

130 lines
2.4 KiB
TypeScript
Raw Normal View History

2016-09-30 00:11:51 +02:00
// @strictNullChecks: true
// @noImplicitAny: true
declare let cond: boolean;
2016-10-03 18:01:53 +02:00
// CFA for 'let' with no type annotation and initializer
2016-09-30 00:11:51 +02:00
function f1() {
let x;
if (cond) {
x = 1;
}
if (cond) {
x = "hello";
}
2016-10-03 19:52:28 +02:00
const y = x; // string | number | undefined
2016-09-30 00:11:51 +02:00
}
2016-10-03 18:01:53 +02:00
// CFA for 'let' with no type annotation and 'undefined' initializer
2016-09-30 00:11:51 +02:00
function f2() {
let x = undefined;
if (cond) {
x = 1;
}
if (cond) {
x = "hello";
}
2016-10-03 19:52:28 +02:00
const y = x; // string | number | undefined
2016-09-30 00:11:51 +02:00
}
2016-10-03 18:01:53 +02:00
// CFA for 'let' with no type annotation and 'null' initializer
2016-09-30 00:11:51 +02:00
function f3() {
let x = null;
if (cond) {
x = 1;
}
if (cond) {
x = "hello";
}
2016-10-03 19:52:28 +02:00
const y = x; // string | number | null
2016-09-30 00:11:51 +02:00
}
2016-10-03 18:01:53 +02:00
// No CFA for 'let' with with type annotation
2016-09-30 00:11:51 +02:00
function f4() {
let x: any;
if (cond) {
x = 1;
}
if (cond) {
x = "hello";
}
2016-10-03 18:01:53 +02:00
const y = x; // any
2016-09-30 00:11:51 +02:00
}
2016-10-03 18:01:53 +02:00
// CFA for 'var' with no type annotation and initializer
2016-09-30 00:11:51 +02:00
function f5() {
var x;
if (cond) {
x = 1;
}
if (cond) {
x = "hello";
}
2016-10-03 19:52:28 +02:00
const y = x; // string | number | undefined
2016-09-30 00:11:51 +02:00
}
2016-10-03 18:01:53 +02:00
// CFA for 'var' with no type annotation and 'undefined' initializer
2016-09-30 00:11:51 +02:00
function f6() {
var x = undefined;
if (cond) {
x = 1;
}
if (cond) {
x = "hello";
}
2016-10-03 19:52:28 +02:00
const y = x; // string | number | undefined
2016-09-30 00:11:51 +02:00
}
2016-10-03 18:01:53 +02:00
// CFA for 'var' with no type annotation and 'null' initializer
2016-09-30 00:11:51 +02:00
function f7() {
var x = null;
if (cond) {
x = 1;
}
if (cond) {
x = "hello";
}
2016-10-03 19:52:28 +02:00
const y = x; // string | number | null
2016-09-30 00:11:51 +02:00
}
2016-10-03 18:01:53 +02:00
// No CFA for 'var' with with type annotation
2016-09-30 00:11:51 +02:00
function f8() {
var x: any;
if (cond) {
x = 1;
}
if (cond) {
x = "hello";
}
2016-10-03 18:01:53 +02:00
const y = x; // any
2016-09-30 00:11:51 +02:00
}
2016-10-03 18:01:53 +02:00
// No CFA for captured outer variables
2016-09-30 00:11:51 +02:00
function f9() {
let x;
if (cond) {
x = 1;
}
if (cond) {
x = "hello";
}
2016-10-03 19:52:28 +02:00
const y = x; // string | number | undefined
2016-09-30 00:11:51 +02:00
function f() {
2016-10-03 18:01:53 +02:00
const z = x; // any
2016-09-30 00:11:51 +02:00
}
}
2016-10-03 18:01:53 +02:00
// No CFA for captured outer variables
2016-09-30 00:11:51 +02:00
function f10() {
let x;
if (cond) {
x = 1;
}
if (cond) {
x = "hello";
}
2016-10-03 19:52:28 +02:00
const y = x; // string | number | undefined
2016-09-30 00:11:51 +02:00
const f = () => {
2016-10-03 18:01:53 +02:00
const z = x; // any
2016-09-30 00:11:51 +02:00
};
}