2

I have installed tsc globally and running tsc index.ts.

Following is my tsconfig.json file in my root project folder.

 {
      "compilerOptions": {
        "target": "esnext",
        "lib": ["dom", "es2017"],
        "allowJs": true,
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "forceConsistentCasingInFileNames": true,
        "module": "es6",
        "outDir": "./build",
        "moduleResolution": "node",
        "resolveJsonModule": true,
        "skipLibCheck": true
    },
    "exclude": ["node_modules", "build"]
    
    }

I am getting the following error when I try to validate whether a number is included in an array.

Property 'includes' does not exist on type 'number[]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2016' or later.

index.ts:-

const max = 15
const min = 10
let arrayOfRandomNumbers: number[] = []

const getTicket = (): void => {
  let randomNumber = null
  if (arrayOfRandomNumbers.length === max - min + 1) {
    document.querySelector('button').disabled = true
  } else {
    while (arrayOfRandomNumbers.length < max - min + 1) {
     
      randomNumber = Math.floor(Math.random() * (max - min + 1)) + min
      if (!arrayOfRandomNumbers.includes(randomNumber)) {
        arrayOfRandomNumbers = [...arrayOfRandomNumbers, randomNumber]
        console.log('arrayOfRandomNumbers for:-', arrayOfRandomNumbers)
        break
      }
    }

  }

}

getTicket()
VLAZ
  • 26,331
  • 9
  • 49
  • 67
ryan
  • 694
  • 9
  • 23
  • Have you checked using Array instead of number[] ? – malarres May 28 '21 at 06:36
  • @malarres it's literally the same thing. It cannot have a difference. – VLAZ May 28 '21 at 06:37
  • Shouldn't be a difference @VLAZ, but the problem itself is weird, and my linting recommends Array over T[]. And maybe that linting process is getting in the way of transpiling – malarres May 28 '21 at 06:38
  • I was able to compile it successfully on ts-playground – boxdox May 28 '21 at 06:39
  • Same error. No difference. – ryan May 28 '21 at 06:39
  • @malarres Your linter is just opinionated. [The two types are the same](https://stackoverflow.com/questions/36842158/arraytype-vs-type-in-typescript). – VLAZ May 28 '21 at 06:40
  • 1
    @ryan [ts-playground](https://www.typescriptlang.org/play?target=99#code/MYewdgzgLgBAtgQwB4wLwwIwFYBQpKxwCWYamADDgDYCmsCATgwgJ4DyAZgEoJgAmIOADkArnABGNBhABcMMGMkMA2gF0yanHnDQYAczoAVIsADWdMgAoAlHIBuIInzQA+GAG8cMGLVjN+gqISUmQKVFReMEQcMJaMzOzcvALCilIQAHS0YHpQABZoqOiIKAC08CQwANSY1h6R3gLAYjRgUBkAjiJSLADKNLTAUCAMlgDk4iJQw2Bj1gCEGXxEEAjitM7oUAzdkQC+MAMQNPXe3gDueUS0sfGsnDwBqcHSWa25BQA88Mgw5cSkGoYOqeM7eBpnfwpIJKMgAWQQ+QyHCoIBGlgRSKhghsMAAVLESn8KoDanUagCId5orF5ndEo9oWlXiRgFQRHwaBBLNjnkprCCqWd6Q9koFmRANBlpSKkk8YekADQwXkKhiqIXefAQEC0LIgPTjWWM8UvSUcEYyUpjZXGsV89LWTUwcQMGgIUxCvYQ737LS+gxQYxmOg2IA) – boxdox May 28 '21 at 06:41
  • Yeah its working fine in ts-playground. Shall i uninstall global eslint? – ryan May 28 '21 at 06:44
  • I have uninstalled global, local and vscode linters. still getting same error :( – ryan May 28 '21 at 06:56
  • I can reproduce it locally. I created an index.ts file with your code and tsconfig.json file with your config. I then installed TS locally with `npm install typescript`. Following that, I ran `npx tsc index.json` from the project directory and got the same error. But `npx tsc` works fine when you don't specify a path. Seems it's not picking up the config file or something. – VLAZ May 28 '21 at 06:57
  • Then i'm out of ideas...jfytk that with your configuration it works for me. Both with `tsc` and `typescript` – malarres May 28 '21 at 06:57
  • OK, weird `npx tsc --showConfig .\index.ts` shows a completely different config (just empty) to `npx tsc --showConfig` which shows the tsconfig.json – VLAZ May 28 '21 at 06:59
  • my commands: 1) `npx typescript` 2) `node ./build/index` result: `arrayOfRandomNumbers for:- [ 1, 2, 3, 11 ]` – malarres May 28 '21 at 06:59
  • @VLAZ bash-3.2$ npx tsc --showConfig ./js/index.ts { "compilerOptions": {}, "files": [ "./js/index.ts" ] } that is why its precisely throwing the error. – ryan May 28 '21 at 07:07
  • 2
    OK, so [the documentation](https://www.typescriptlang.org/docs/handbook/compiler-options.html) has kind of an answer - running `tsc ` will only run against one file using the compiler defaults. But running `tsc` without a file argument will use the project configuration. So, it's documented but seems a very strange behaviour to have. So if you want a single file, you can specify command line configuration `npx tsc --target es2016 .\index.ts` but you cannot seem to specify a config file `npx tsc --project .\tsconfig.json .\index.js` does not work for single files. – VLAZ May 28 '21 at 07:07
  • @VLAZ you are right. tsc --target es2016 js/index.ts works. But this is weird. It should pickup the target and lib from tsconfig.json. – ryan May 28 '21 at 07:11
  • 1
    anyway it will transpile everything on a given folder, so maybe the solution is cd ./js && tsc – malarres May 28 '21 at 07:12
  • @ryan I agree that *I would expect* the project configuration to be used. However, the documentation for the compiler explicitly says it doesn't. Yes, I do find this design weird but it isn't a bug, apparently. – VLAZ May 28 '21 at 07:13
  • If you really need to compile one file rather than the whole project, then there is a workaround [described here](https://stackoverflow.com/a/44748041/) - you can create a different tsconfig file, e.g., tsconfig-only-index.ts.json and in it specify it extends the project tsconfig.json file and in the `includes` section list only index.ts. So, it's a configuration that extends the project one but only acts on one file. You can then use it with `tsc --project tsconfig-only-index.ts.json` – VLAZ May 28 '21 at 07:23
  • Does this answer your question? [How to compile a specific file with tsc using the paths compiler option](https://stackoverflow.com/questions/44676944/how-to-compile-a-specific-file-with-tsc-using-the-paths-compiler-option) – VLAZ May 28 '21 at 07:23
  • 1
    Does this answer your question? [Property 'includes' does not exist on type 'string\[\]'](https://stackoverflow.com/questions/40545329/property-includes-does-not-exist-on-type-string) – kaya3 Jan 18 '22 at 14:47

1 Answers1

-3

Math.floor() will never return an array, that is why you next line is failing. includes method is for array not for number.

randomNumber = Math.floor(Math.random() * (max - min + 1)) + min // 

There must be an array that need to assign in the randomNumber, so you can use that method, now it returning a number, that the reason you are getting that error.

Abhinav Kumar
  • 2,883
  • 1
  • 17
  • 30
  • 2
    `includes` is being called on the number array. It's `Array.includes(value)`, and the implementation is correct. – boxdox May 28 '21 at 06:34
  • includes is called on array. randomNumber will generate a valid number which i am validating whether it exists in the array arrayOfRandomNumbers. – ryan May 28 '21 at 06:35
  • Just to add. I dont have any package.json. Its not a node project. I am using typescript file (index.ts) which is compiled into javascript file (index.js) using global typescript. And then that javascript is called in index.html using script tag. – ryan May 28 '21 at 06:38