microsoft/TypeScript-React-Starter

Why `export const INCREMENT_ENTHUSIASM` then `export type INCREMENT_ENTHUSIASM` again

yeongjet opened this issue · 2 comments

image
Why export const INCREMENT_ENTHUSIASM then export type INCREMENT_ENTHUSIASM again, what's the meaning?

You can open src/actions/index.tsx to understand more.

Hi @yeongjet, have a look at typeof type guards and string literal types.

Basically, it means a variable that has the type INCREMENT_ENTHUSIASM can only has 'INCREMENT_ENTHUSIASM' as a value (in other words, it can't only be a string, it must also has this specific text as a value).

It's the same as writing export type INCREMENT_ENTHUSIASM = 'INCREMENT_ENTHUSIASM'.

On line 2 value INCREMENT_ENTHUSIASM is exported.
On line 3 type INCREMENT_ENTHUSIASM is exported.

TypeScript is smart enough to know when a variable is used as a value, and when it is used as a type.