Skip to content

Instantly share code, notes, and snippets.

@jamesona jamesona/enum_demo.ts
Last active Mar 4, 2019

Embed
What would you like to do?
use string enums and symbols as index types
enum Stats {
int = 'int does stuff',
str = 'str does other stuff',
def = 'def prevents stuff',
}
type StatName = keyof typeof Stats
interface HasStats {
stats: {
[key in StatName]: number
}
}
class Thing implements HasStats {
stats = {
int: 20,
str: 20,
def: 20
}
}
const int = Symbol('int')
const str = Symbol('str')
const def = Symbol('def')
type StatName = typeof int | typeof str | typeof def
interface HasStats {
stats: {
[key in StatName]: number
}
}
class Thing implements HasStats {
stats = {
[int]: 20,
[str]: 20,
[def]: 20
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.