Skip to content

Instantly share code, notes, and snippets.

@wilmoore
Last active May 21, 2024 06:31
Show Gist options
  • Select an option

  • Save wilmoore/9625fad881e7e2c390a3f6c8e186b710 to your computer and use it in GitHub Desktop.

Select an option

Save wilmoore/9625fad881e7e2c390a3f6c8e186b710 to your computer and use it in GitHub Desktop.
Software Engineering :: Programming :: Languages :: JavaScript :: TypeScript :: Enum

Software Engineering :: Programming :: Languages :: JavaScript :: TypeScript :: Enum

⪼ Made with 💜 by Polyglot.

Alternative

POJO as const

as const means the object cannot be maniuplated or changed

const LOG_LEVEL = {
  DEBUG: 'DEBUG',
  WARNING: 'WARNING',
  ERROR: 'ERROR'
} as const;

The "as const" syntax in TypeScript is used to treat elements of an array or properties of an object as constants, i.e., their values cannot be changed. More importantly, it performs a type narrowing, indicating to the TypeScript compiler that the value should be inferred as narrowly as possible rather than its wider possible type.

const array = [1, 2, 3] as const; // The type is readonly [1, 2, 3], not number[]
const obj = { key: "value" } as const; // The type is { readonly key: "value" }, not { key: string }

YouTube Research

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment