Last active
March 4, 2019 22:52
-
-
Save jamesona/513a32c4d18ff8292a2b9d8e82d779a9 to your computer and use it in GitHub Desktop.
use string enums and symbols as index 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
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 | |
} | |
} |
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
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