craftsman
craftsman

Reputation: 15653

Is "type" a keyword in JavaScript?

I just came across somebody using "type" in a piece of ES6 code.

export type Action =
  {
    type: 'todo/complete',
    id: string,
  } |
  {
    type: 'todo/create',
    text: string,
  } |
  {
    type: 'todo/destroy',
    id: string,
  } |
  {
    type: 'todo/destroy-completed',
  } |
  {
    type: 'todo/toggle-complete-all',
  } |
  {
    type: 'todo/undo-complete',
    id: string,
  } |
  {
    type: 'todo/update-text',
    id: string,
    text: string,
  };

Couldn't find anything that sheds light on it. Is it a keyword? What does it exactly do?

Upvotes: 42

Views: 24541

Answers (3)

linusthe3rd
linusthe3rd

Reputation: 3566

As mentioned by PitaJ, the type symbol here is not part of ES6 or any earlier version of JavaScript, but rather part of the Flow static type checker.

Here are the docs for the type symbol.

Upvotes: 30

Nver Abgaryan
Nver Abgaryan

Reputation: 621

You can disable it

export interface Body {
   a: string
   b: string
   c: string
   // tslint:disable-next-line:no-reserved-keywords
   type: string
   f: string
   e: string
}

Upvotes: 2

arcyqwerty
arcyqwerty

Reputation: 10695

As far as I know, the ES6 spec does not list it as a reserved keyword.

The following tokens are ECMAScript keywords and may not be used as Identifiers in ECMAScript programs.

break do in typeof case else instanceof var catch export new void class extends return while const finally super with continue for switch yield debugger function this default if throw delete import try

Upvotes: 6

Related Questions