Skip to content

Instantly share code, notes, and snippets.

@jamesona
Last active March 4, 2019 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesona/513a32c4d18ff8292a2b9d8e82d779a9 to your computer and use it in GitHub Desktop.
Save jamesona/513a32c4d18ff8292a2b9d8e82d779a9 to your computer and use it in GitHub Desktop.
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