简体   繁体   中英

tsc and ts-node ignoring noImplicitAny

I have a NodeJS project with @types/node , ts-node , and typescript installed as dev dependencies, and in my tsconfig.json file I have set "noImplicitAny": true . In my package.json file, I have three scripts:

"start": "npm run build && node out/index.js",
"test": "nodemon",
"build": "rimraf ./out && tsc"

rimraf basically deletes a folder, in this case, the output directory.

here's my nodemon config:

{
  "watch": [
    "src"
  ],
  "ext": ".ts,.js",
  "ignore": [],
  "exec": "ts-node --project ./tsconfig.json ./src/index.ts"
}

Now here's my code:

let text = 'hi'
console.log(text)

And node test|start|run build all run successfully. Surely, with noImplicitAny set, TypeScript shouldn't allow me to set the text variable without specifying it as string ?

Sorry if this seems like a stupid question, I'm very new to typescript.

Thanks!

When you define and initialize a variable at the same time, TypeScript implicitly assigns the type from type of the initialized value, so it's not an implicit any , it's an implicit string , since 'hi' is a string and only a string .

These two statements are identical to TypeScript:

let text: string = 'hi';
let text = 'hi';

In fact, some linters will have rules like no-unnecessary-type-assertion that prevent you from adding a type assertion that doesn't change the type.

An example of an implicit any is something like this:

const func = (test) => alert(test);
--------------^^^^ implicit any type

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM