Last active
September 5, 2022 16:05
-
-
Save jhonsore/23c9c8dd7254b2cb475226e88b3e8319 to your computer and use it in GitHub Desktop.
JS typescript enum with types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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