Skip to content

Instantly share code, notes, and snippets.

@prozacgod
Created March 23, 2017 19:25
Show Gist options
  • Save prozacgod/b8f71ef58adb63b028ee6ee877e8587f to your computer and use it in GitHub Desktop.
Save prozacgod/b8f71ef58adb63b028ee6ee877e8587f to your computer and use it in GitHub Desktop.
What is the typeof a class definition in typescript ?
class UserModel {
constructor(opt) {
if (opt.id) {
}
}
get name() {
return 'Prozacgod'
}
get rank() {
return 'god';
}
}
interface IUser {
name: string;
};
interface IModels {
UserModel: UserModel;
}
interface IApiContext {
// this would be the current logged in user
user: IUser;
models: IModels;
};
// In my code these api calls are isolated through a native JS library, so typeings get lost
// I need models to be in the ctx argument
let api = {
logUser(ctx:IApiContext) {
console.log(ctx.user.name);
},
getUser(ctx:IApiContext, id:number) {
return new ctx.models.UserModel({id});
}
};
function fakeCallApiFunction() {
// declare the ctx the code is called with
var ctx: IApiContext = {
user: {name:'Prozacgod'},
models: {
UserModel
}
};
api.logUser(ctx);
let user = api.getUser(ctx, 1);
console.log(`${user.name} <${user.rank}>`);
}
fakeCallApiFunction();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment