Last active
June 20, 2018 15:02
-
-
Save Snugug/72a6181e6b641d9e32df126135c0ae0c to your computer and use it in GitHub Desktop.
Need a way to type provider:SOMETHING so I can `new` its children
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
import { validate, IsInt, IsEmail, IsIn, IsNotEmpty } from 'class-validator'; | |
import { Provider } from './provider'; | |
export class InstanceConfig { | |
[key:string]: number | string; | |
@IsEmail() | |
@IsNotEmpty() | |
email: string; | |
@IsInt() | |
@IsNotEmpty() | |
duration: number; | |
} | |
export class InstanceInstance { | |
[key:string]: number | string | boolean; | |
@IsNotEmpty() | |
template: string; | |
@IsNotEmpty() | |
@IsIn(['emea', 'us-central', 'apac', 'aus-sydney']) | |
region: string; | |
@IsNotEmpty() | |
@IsIn(['default', 'on', 'off']) | |
internetAccess: string; | |
@IsNotEmpty() | |
smartrdp: boolean; | |
@IsNotEmpty() | |
@IsInt() | |
idleRuntimeLimit: number; | |
@IsNotEmpty() | |
@IsIn(['shutdown', 'off', 'suspend']) | |
idleRuntimeType: string; | |
@IsNotEmpty() | |
@IsInt() | |
totalRuntimeLimit: number; | |
} | |
export class InstanceProvider implements Provider<InstanceConfig, InstanceInstance> { | |
Config: new() => InstanceConfig; | |
Instance: new() => InstanceInstance; | |
} |
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
export interface Provider<ProviderConfig, ProviderInstance> { | |
Config: new () => ProviderConfig; | |
Instance: new () => ProviderInstance; | |
} |
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
import { InstanceProvider, InstanceConfig, InstanceInstance } from './skytap'; | |
import { Provider } from './provider'; | |
import { validate } from 'class-validator'; | |
async function foo<ProviderConfig, ProviderInstance>(provider: Provider<ProviderConfig, ProviderInstance>) { | |
const config = new provider.Config(); | |
config.email = 'foogmail.com'; | |
config.duration = 10; | |
let errs = await validate(config, {whitelist: true}); | |
console.log(errs); | |
} | |
foo<InstanceConfig, InstanceInstance>(new InstanceProvider()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think I've narrowed it down to https://gist.github.com/Snugug/72a6181e6b641d9e32df126135c0ae0c#file-use-js-L5, specifically that even though I believe I'm passing the types in properly for the generic, it's not actually picking up the structure inside the function. I can confirm that this in general works because if I useInstanceProvider
outside of the function call it all works as expectedWhile that doesn't work,
InstanceProvider.Config
is not a constructor so I can'tnew
it