noImplicitAny 不工作打字稿
noImplicitAny not working Typescript
在我的根文件夹中,我有 tsconfig.json 文件,如下所示:
{
"compilerOptions": {
/* Basic Options */
"target": "es2015", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": [
"es2017",
"dom"
], /* Specify library files to be included in the compilation. */
"declaration": false, /* Generates corresponding '.d.ts' file. */
"outDir": "./dist", /* Redirect output structure to the directory. */
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
"strictNullChecks": true, /* Enable strict null checks. */
"strictFunctionTypes": true, /* Enable strict checking of function types. */
/* Additional Checks */
"noUnusedLocals": true, /* Report errors on unused locals. */
"moduleResolution": "node",
"typeRoots": [
"node_modules/@types"
], /* List of folders to include type definitions from. */
"experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
"emitDecoratorMetadata": true /* Enables experimental support for emitting type metadata for decorators. */
}
}
在我的 src 文件中,我有这样的文件:
export class CompareDatesLogic implements ICRUD {
public create() {
let test;
test = 'aaaa';
return {
name: test,
};
}
}
但 Visal Code 未检测到 noImplicitAny
,有人知道这可能是什么问题吗?
查看 https://github.com/Microsoft/TypeScript/issues/18679 / and https://github.com/Microsoft/TypeScript/pull/11263 了解更多详情,
简而言之,TS 编译的新功能允许控制流分析来确定最初未分配的值的类型。
在你的代码中,
let test; //no types defined
test = 'aaa' //assigned string
并且在这两次执行之间,test
的类型没有任何变化,tsc 通过分析代码自动推断出 test:string
。
我借用了问题中的详细示例:
function f(cond: boolean) {
let x;
if (cond) {
x = "hello";
x; // string
}
else {
x = 123;
x; // number
}
return x; // string | number
}
function g(cond: boolean) {
let y = null;
if (cond) {
y = "hello";
}
return y; // string | null
}
描述其工作原理。
在我的根文件夹中,我有 tsconfig.json 文件,如下所示:
{
"compilerOptions": {
/* Basic Options */
"target": "es2015", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": [
"es2017",
"dom"
], /* Specify library files to be included in the compilation. */
"declaration": false, /* Generates corresponding '.d.ts' file. */
"outDir": "./dist", /* Redirect output structure to the directory. */
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
"strictNullChecks": true, /* Enable strict null checks. */
"strictFunctionTypes": true, /* Enable strict checking of function types. */
/* Additional Checks */
"noUnusedLocals": true, /* Report errors on unused locals. */
"moduleResolution": "node",
"typeRoots": [
"node_modules/@types"
], /* List of folders to include type definitions from. */
"experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
"emitDecoratorMetadata": true /* Enables experimental support for emitting type metadata for decorators. */
}
}
在我的 src 文件中,我有这样的文件:
export class CompareDatesLogic implements ICRUD {
public create() {
let test;
test = 'aaaa';
return {
name: test,
};
}
}
但 Visal Code 未检测到 noImplicitAny
,有人知道这可能是什么问题吗?
查看 https://github.com/Microsoft/TypeScript/issues/18679 / and https://github.com/Microsoft/TypeScript/pull/11263 了解更多详情,
简而言之,TS 编译的新功能允许控制流分析来确定最初未分配的值的类型。
在你的代码中,
let test; //no types defined
test = 'aaa' //assigned string
并且在这两次执行之间,test
的类型没有任何变化,tsc 通过分析代码自动推断出 test:string
。
我借用了问题中的详细示例:
function f(cond: boolean) {
let x;
if (cond) {
x = "hello";
x; // string
}
else {
x = 123;
x; // number
}
return x; // string | number
}
function g(cond: boolean) {
let y = null;
if (cond) {
y = "hello";
}
return y; // string | null
}
描述其工作原理。