'as const' for making objects strictly typed
You can use as const
in typescript to restrict an object's types.
const status = {
SUCCESS: 'success',
ERROR: 'error',
LOADING: 'loading',
} as const;
Without using as const
, both the keys and values of the object would be string types, but
with as const
, the value types are the exact literals:
"success" | "error" | "loading"
Tweet