-
Notifications
You must be signed in to change notification settings - Fork 286
Passing a model to a function and accessing static members #232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Seems this might be a deeper issue in TypeScript: microsoft/TypeScript#5843 (comment) |
You can achieve this like so: type NonAbstract<T> = {[P in keyof T]: T[P]}; // "abstract" gets lost here
type Constructor<T> = (new () => T);
type NonAbstractTypeOfModel<T> = Constructor<T> & NonAbstract<typeof Model>;
function createResource<T extends Model<T>>(model: NonAbstractTypeOfModel<T>): Promise<T[]> {
return model.findAll<T>();
} Now you can use it without passing any generic type explicitly. The proper type is inferred automatically by typescript: createResource(User)
.then(users => users[0]. ...) // <-- autocompletion should work very well here
; |
How obvious, thanks! This solves my issue, but FYI, yours gives:
So you need to |
You need to use bluebird as the promise type: |
I'm trying to write a function that takes a model class and calls
.findAll({})
, but TypeScript is giving me an error. This reproduces it:gives:
The text was updated successfully, but these errors were encountered: