Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error TS2304: Cannot find name 'InputEvent'

As described in Does TypeScript have type definitions for InputEvent?, I tried to use @types/dom-inputevent to obtain InputEvent in my Angular 7 project. But it shows error TS2304: Cannot find name 'InputEvent' whenever I use it.

Is there any need to "register" it in the Angular project or is it a bug?

If a fix is not available, I just want a convenient way to achieve the same result as event.data in InputEvent.

Thanks!

like image 549
Zenas Chen Avatar asked Dec 05 '18 08:12

Zenas Chen


1 Answers

I had the same problem trying to use @types/dom-inputevent in unit tests for an Angular 6 app. In my case, the cause was that I had an explicit 'types' entry in my tsconfig.spec.js, as outlined below.

tsconfig.json (irrelevant parts omitted)

{
  "compilerOptions": {
     "typeRoots": [
       "node_modules/@types"
     ]
  }
}

tsconfig.spec.js (irrelevant parts omitted)

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
  "types": [
    "jasmine",
    "node"
  ]
}

As you can see, there's a types entry that lists explicit types that should be made available during compilation. This overrides the typeRoots setting in the parent tsconfig.json, so only @types/jasmine and @types/node were available when compiling my unit tests. See also here: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#types-typeroots-and-types.

Solution is to add dom-inputevent to the types list e.g.

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
  "types": [
    "jasmine",
    "node",
    "dom-inputevent"
  ]
}

Another approach would have been to remove the types entry altogether. If I did that, typeRoots would have kicked in and looked for all types under node_modules/@types, thus including @types/dom-inputevent too.

like image 131
Dan C. Avatar answered Nov 04 '22 18:11

Dan C.