2

I have this line of code

import { Observable, throw} from 'rxjs';

I'm getting the error identifier expected:

ERROR in config/config.service.ts(3,22): error TS1003: Identifier expected.

It indicated the error is stemming from that line/ position 22, which is the start of the throw word.

What do i need to do in order to resolve this issue? what do they mean by an identifier?

falcon-valley
  • 31
  • 1
  • 1
  • 7

3 Answers3

4

I came across this looking to resolve the same error, but with a different cause. In case anyone else is having the same, my issue was that I converted some dot-notation references to string notation, but forgot to get rid of the dots!

Example:

// Started with ...   
const variableName = someObject.dynamicPropertyName;
// Was changed to ...
const variableName = someObject.['dynamicPropertyName'];
// Instead of ...
const variableName = someObject['dynamicPropertyName'];

In my case, it was just a bad find-replace-issue. But I imagine that someone may run into this in other situations as well.

Wayne F. Kaskie
  • 3,257
  • 5
  • 33
  • 43
2

First, identifiers are the names that you give to your variables, functions, etc. It's the official term for them and carries with it the rules for which characters are valid within them.

In most cases, though, an identifier cannot be the same name as a reserved word – the names used by the language itself, such as control structures (if, for, etc.).

let for;
// SyntaxError at `for`

throw is one such reserved word, which represents a statement for raising custom errors, and cannot be used as a variable defined by import.

To get around this, you can specify your own name/identifier (alias) for rx.js' throw with as.

const { Observable, throw as rxThrow } from 'rxjs';
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
  • I tried using your suggestion and the error messaged changed to - "ERROR in config/config.service.ts(3,45): error TS1141: String literal expected. config/config.service.ts(44,12): error TS2304: Cannot find name 'throwError'." on line 44 i am basically returning an error using the throwError function – falcon-valley May 27 '18 at 16:07
  • @falcon-valley Sorry. Forgot the quotes for `'rxjs'`. Edited the snippet. – Jonathan Lonowski May 27 '18 at 16:08
  • @falcon-valley For the 2nd error, with [`throwError`](https://rxjs-dev.firebaseapp.com/api/index/throwError), the function will need to be included in your `import` statement – `import { ..., throwError } from 'rxjs'`. Otherwise, your script has no awareness of the function's existence, which is why it's raising `cannot find name`. – Jonathan Lonowski May 27 '18 at 16:21
-1

throw is a reserved word, try:

import { Observable } from 'rxjs';
import 'rxjs/add/observable/throw';
paul
  • 21,653
  • 1
  • 53
  • 54
  • i used your suggestion and the error messaged changed to = "ERROR in config/config.service.ts(45,12): error TS2304: Cannot find name 'throwError' " based on the line = "return throwError( 'Something bad happened; please try again later.'); };" – falcon-valley May 27 '18 at 16:00
  • adding the code around line 45 to your question might help – paul May 27 '18 at 16:08
  • yes but it is a different error i am getting from the original one i posted, i was getting only the error i posted originally. when i used your suggestion i started getting errors for the code on line 45 also. – falcon-valley May 27 '18 at 16:11
  • Yes, the import problem is solved (line 3). You now have another problem (line 45). – paul May 27 '18 at 16:12
  • yea the link you posted helped me solve the problem, but i had to change some things like the throwError word in the line 45 to _throw. – falcon-valley May 27 '18 at 18:16