-
-
Save Arcath/7fed37aca61499de124a205daf476079 to your computer and use it in GitHub Desktop.
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
type ProcessTypes = | |
"init" | | |
"move" | |
type MetaData = { | |
"init": {} | |
"move": { | |
creep: string | |
pos: { | |
x: number | |
y: number | |
roomName: string | |
} | |
range: number | |
path?: string | |
lastPos?: [number, number, string] | |
stuck?: number | |
} | |
} | |
addProcess<T extends ProcessTypes>(processType: T, name: string, priority: number, metaData: MetaData[T]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You wanna see something even nicer?
https://www.typescriptlang.org/play/#src=%2F%2F%20type%20ProcessTypes%20%3D%20%0D%0A%2F%2F%20%20%20%22init%22%20%7C%0D%0A%2F%2F%20%20%20%22move%22%0D%0A%0D%0Aenum%20ProcessTypes%20%7B%0D%0A%20%20%20%20Init%20%3D%20'init'%2C%0D%0A%20%20%20%20Move%20%3D%20'move'%0D%0A%7D%0D%0A%0D%0Atype%20MetaData%20%3D%20%7B%0D%0A%20%20%22init%22%3A%20%7B%7D%0D%0A%20%20%22move%22%3A%20%7B%0D%0A%20%20%20%20creep%3A%20string%0D%0A%20%20%20%20pos%3A%20%7B%0D%0A%20%20%20%20%20%20x%3A%20number%0D%0A%20%20%20%20%20%20y%3A%20number%0D%0A%20%20%20%20%20%20roomName%3A%20string%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20range%3A%20number%0D%0A%20%20%20%20path%3F%3A%20string%0D%0A%20%20%20%20lastPos%3F%3A%20%5Bnumber%2C%20number%2C%20string%5D%0D%0A%20%20%20%20stuck%3F%3A%20number%0D%0A%20%20%7D%0D%0A%7D%0D%0A%0D%0Adeclare%20function%20addProcess%3CT%20extends%20ProcessTypes%3E(processType%3A%20T%2C%20name%3A%20string%2C%20priority%3A%20number%2C%20metaData%3A%20MetaData%5BT%5D)%0D%0A%0D%0AaddProcess(ProcessTypes.Init%2C%20'foo'%2C%2010%2C%20%7B%7D)%3B%20%2F%2F%20good%0D%0AaddProcess(ProcessTypes.Move%2C%20'bar'%2C%209%2C%20%7B%7D)%3B%20%2F%2F%20bad%2C%20missing%20metadata
It works with enums too!
While I like literal string types in TypeScript, I always felt that they were there to account for string based enums to be an idiomatic thing in JavaScript. With actual string-valued Enums in TypeScript, things get nicer.