Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

noImplicitAny not working Typescript

In my root folder I have tsconfig.json file which looks like this:

{
  "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. */
  }
}

And inside my src filder I have file where I have something like this:

export class CompareDatesLogic implements ICRUD {

    public create() {
        let test;

        test = 'aaaa';
        return {
            name: test,
        };
    }
}

But Visal Code not detecting noImplicitAny, anyone know what can be a problem?

like image 226
Vladimir Djukic Avatar asked Mar 02 '18 15:03

Vladimir Djukic


1 Answers

Check out https://github.com/Microsoft/TypeScript/issues/18679 / and https://github.com/Microsoft/TypeScript/pull/11263 for further detail,

In short, new feature of TS compile allows control flow analysis to determine type of values not assigned at first.

in your code, let test; //no types defined test = 'aaa' //assigned string

and between those two execution there's nothing mutates type of test, tsc auto infers test:string by analyzing code.

I'm borrowing detailed example in issue:

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
}

describes how this works.

like image 134
OJ Kwon Avatar answered Oct 14 '22 03:10

OJ Kwon