3

I am using next.js , react, and typescript.
When I use switch as a variable name, I get a typescript error.
How can I avoid the error?

'switch' is not allowed as a variable declaration name.ts(1389)
const Template: Story = (args: AtomSwitchProps) => <Switch {...args} />;

export const switch = Template.bind({});
switch.args = {
};

2
  • 2
    switch is reserverd word in js. use different name Commented Apr 2, 2021 at 6:10
  • 1
    The error message seems pretty clear - that name is not allowed, so you have to use a different name. Commented Apr 2, 2021 at 6:13

1 Answer 1

4

You can't use a keyword as an identifier in your JavaScript programs. Switch is a Reserved keyword. Reserved keywords are used to perform internal operations. Here's a list of keywords of JavaScript.

Sign up to request clarification or add additional context in comments.

4 Comments

Is there any way to avoid it?
Yeah, by changing the name of the variable
Link is broken, there's a new one: w3schools.com/js/js_reserved.asp
For my use-case, I found a trick to use "default" as a string during the return, instead of returning the const: Original code that returned an error: const default = null; return { default }; Returning a string directly works: return { "default": null };

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.