menu

Questions & Answers

can someone clarify rxjs observable import error

I am using angular 13 and rxjs 7.4.0. and vscode......when I do this:

import { Observable } from 'rxjs';

I get this error:

Could not find a declaration file for module 'rxjs'. '/Applications/GenysisBuild/genysis-jta/node_modules/rxjs/dist/cjs/index.js' implicitly has an 'any' type.
  Try `npm install @types/rxjs` if it exists or add a new declaration (.d.ts) file containing `declare module 'rxjs';

Now this error has been popping up in prior versions with no clear solution. There is also the option to do this:

import { Observable } from 'rx'

and install this: @types/rx

then the error goes away but I really have no idea what 'rx' is relative to "rxjs"

can someone please clarify this issue?....how do I get rid of this error using "rxjs"?

EDIT: tsconfig:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitOverride": true,
    "noPropertyAccessFromIndexSignature": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2017",      ....also tried 2015
    "module": "es2020",      ....also tried esnext
    "lib": [
      "es2020",
      "dom"
    ],
    "resolveJsonModule": true,    ..with or without
    "esModuleInterop": true      ...with or without
  },
  "angularCompilerOptions": {
    "enableI18nLegacyMessageIdFormat": false,
    "strictInjectionParameters": true,
    "strictInputAccessModifiers": true,
    "strictTemplates": true
  }
}

also tried this:

 "typeRoots": [
 "node_modules/@types"
  ],
Comments:
2023-01-18 09:42:02
That's strange, it is trying to import from dist/cjs/index.js when it should be going to src/index.ts which points to src/internal/Observable.ts. Could you please try importing from rxjs/index and/or rxjs/internal/Observable and confirm these files are present in node_modules/rxjs? Most likely it's a configuration in your tsconfig.json. You can uninstall the @types/rx as the types are bundled with rxjs.
2023-01-18 09:42:02
Try removing your node_modules directory and install everything from scratch.
2023-01-18 09:42:02
@ChrisHamilton neither /index or /internal/Observable made a difference. I didn't see anything unusual in the tsconfig but I added it the question.
2023-01-18 09:42:02
@martin .....Hey Martin.....this doesn't work either.
2023-01-18 09:42:02
I still don't have an answer as to what cause this problem but upgrading vscode to 1.65.2 seems to do the trick.
Answers(1) :

Couldn't find the root cause, but after some experimentation I've found that adding in tsconfig.json the following lines seems to fix the issue on IntelliJ, with "typescript": "~4.8.4" and "rxjs": "^7.8.0" versions:

"target": "esnext",
"typeRoots": [
  "./node_modules/@types",
  "./node_modules/dist/types/operators/index.d.ts"
],
"paths": {
  "rxjs": ["./node_modules/dist/esm/index"]
}