Skip to content

Instantly share code, notes, and snippets.

@jhonsore
Last active September 5, 2022 16:05
Show Gist options
  • Save jhonsore/23c9c8dd7254b2cb475226e88b3e8319 to your computer and use it in GitHub Desktop.
Save jhonsore/23c9c8dd7254b2cb475226e88b3e8319 to your computer and use it in GitHub Desktop.
JS typescript enum with types
// Using a model as a type
const taco = 'taco';
type TacoType = typeof taco;
// type TacoType = 'taco'
let bell = 'bell';
type BellType = typeof bell;
// type BellType = string
//--------
const people = () => ({
name: 'Jhon',
age: 32
});
let people1: ReturnType<typeof people> = {
name: 'Peter',
age: 40
}
// let people1: { name: string, age: number }
//--------
const person = {
name: 'Tom',
age: 30,
country: 'BR'
} as const;
type Keys = keyof typeof person
// type Keys = "name" | "age" | "country"
type Values = typeof person[Keys]
// type Values = "Tom" | 30 | "BR
enum NAMES {
cnpj='cnpj',
site='site',
description='description'
}
type TNames = {
[NAMES.site]: string,
[NAMES.cnpj]: string,
[NAMES.description]: string
}
const t:TNames = { site:'', description:'', cnpj: ''}
t[NAMES.site] = 'teste'
t.site = 'teste'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment