Created
March 28, 2020 15:21
-
-
Save endel/3199440b057092b6d4c9f33161f03667 to your computer and use it in GitHub Desktop.
Extract parameters of an instance method using TypeScript
This file contains hidden or 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
// Playground link: https://www.typescriptlang.org/play/?ssl=1&ssc=1&pln=19&pc=49#code/C4TwDgpgBAKuEB4YD4oF4oDsIHcoAoA6YgQwCcBzAZwC4oTMQBtAXQEp1UYBuAKF4DGAGxJUqUAEoB7KQFsk6eo1QBvXlChVgJYBAD8dHuqhTMAYTIQdEfFLDAAlqdpKQHNRoC+vb4JFioAFkQaTkoCAAPXUwAE3FQ+RUYhwAzFIhLTGAAcQhsMgcBOi0CzApPVWNTCytdW3snTBcVLBJZCGLgUooAGighCAA3CCE6TABXWQAjDKhPd29fFPHMAUdTKBiIFIdsBUjouNh4BATkZHxjMhlZOEhDHqqG5zoABXI2iF0yKgQASSa2lWEDuiBQTAA5NVLNYISxkEwAAwsXjuHz8LY7bD4YIJPotTCfOgAIj+wCgOCkZAA1lQAITEvoDYajKAARjmbG4QA | |
type Type<T> = new (...args: any[]) => T; | |
class Room<T = any> { | |
state?: T; | |
onCreate(options: any) { | |
} | |
} | |
class MyRoom extends Room<{differentGeneric: string}> { | |
onCreate(options: { name: string, level: number }) {} | |
} | |
function define<T extends Type<Room>>( | |
roomType: T, | |
options: Parameters<InstanceType<T>['onCreate']>[0] | |
) { | |
} | |
define(MyRoom, { name: "It works!", level: 1 }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment