Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript noImplicitAny编译器标志的范围是什么_Typescript - Fatal编程技术网

Typescript noImplicitAny编译器标志的范围是什么

Typescript noImplicitAny编译器标志的范围是什么,typescript,Typescript,TypeScript文档将noImplicitAny编译器标志记录到 使用隐含的any类型在表达式和声明上引发错误 因此,在以下代码中: let x; // x is of implicitly of type `any`, but no error function foo(y) { // error: parameter 'y' implicitly has an 'any' type. let z; // z is of implicitly

TypeScript文档将
noImplicitAny
编译器标志记录到

使用隐含的
any
类型在表达式和声明上引发错误

因此,在以下代码中:

let x;            // x is of implicitly of type `any`, but no error

function foo(y) { // error: parameter 'y' implicitly has an 'any' type. 
    let z;        // z is of implicitly of type `any`, but no error
}

x
z
是否也应标记为隐式键入到
any

这实际上是由于在版本2.1中进行了修复。在此之前,您的代码可能会抛出错误

从:

在TypeScript 2.1中,TypeScript将不只是选择任何选项 根据以后分配的内容推断类型

示例:

let x;

// You can still assign anything you want to 'x'.
x = () => 42;

// After that last assignment, TypeScript 2.1 knows that 'x' has type '() => number'.
let y = x();

// Thanks to that, it will now tell you that you can't add a number to a function!
console.log(x + y);
//          ~~~~~
// Error! Operator '+' cannot be applied to types '() => number' and 'number'.

// TypeScript still allows you to assign anything you want to 'x'.
x = "Hello world!";

// But now it also knows that 'x' is a 'string'!
x.toLowerCase();
因此,在您的情况下,TypeScript实际上会根据您分配给它的内容推断类型:

function foo(y) { 
    let z;
    z = "somestring";
    z.toUpperCase(); // z is string now. No error;
    z = 10;
    z.toUpperCase(); // z is number now; Error
}

非常感谢。因此,只要
x
分配了一个值,它就会获取该值的类型,即不是
any
x
将保留该类型,直到为其分配了不同类型的值,从该值开始,它将采用新值的类型。