-
-
Notifications
You must be signed in to change notification settings - Fork 26
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
refactor: refactor types #261
Conversation
Pull Request Test Coverage Report for Build 9732452201Details
💛 - Coveralls |
Pull Request Test Coverage Report for Build 9797301066Details
💛 - Coveralls |
Pull Request Test Coverage Report for Build 9817607131Details
💛 - Coveralls |
Pull Request Test Coverage Report for Build 9819663515Details
💛 - Coveralls |
@@ -21,6 +21,7 @@ class Model<T> extends EventEmitter { | |||
Document: { new<T>(data: T): Document<T> }; | |||
Query: { new<T>(data: Document<T>[]): Query<T> }; | |||
_database: Database; | |||
[key : string]: any; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lines 79 to 82 in af981d3
Object.assign(this, schema.statics); | |
// Apply instance methods | |
Object.assign(_Document.prototype, schema.methods); |
Solve the above code, but as with #252, it's not a good solution. Model knows nothing about static and method types in Schema.
@SukkaW Do you have any good solutions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was going to add another generic for setting the type of static and method, but its not possible to act directly on the class.
interface Extra {
statics: Record<string, (...args: any[]) => any>;
methods: Record<string, (...args: any[]) => any>;
}
type AddThis<R extends Record<string, (...args: any[]) => any>, T> = {
[K in keyof R]: (this: T, ...args: Parameters<R[K]>) => ReturnType<R[K]>;
}
class Schema<T = any, K extends Partial<Extra> = Extra> {
statics!: K['statics'] extends Record<string, (...args: any[]) => any> ? AddThis<K['statics'], T> : Record<string, (this: T, ...args: any[]) => any>;
methods!: K['methods'] extends Record<string, (...args: any[]) => any> ? AddThis<K['methods'], T> : Record<string, (this: T, ...args: any[]) => any>;
}
interface AssetSchema {
_id: string
}
const a = new Schema<AssetSchema, {
statics: {
add: (a: number, b: number) => number;
}
}>().statics.add // <-- (this: AssetSchema, a: number, b: number) => ReturnType<(a: number, b: number) => number>
const b = new Schema<AssetSchema>().methods // <-- AddThis<Record<string, (...args: any[]) => any>, AssetSchema>
class Model<T, K extends Partial<Extra> = Extra> {
schema!: Schema<T, K>
// [key in keyof K['statics']]: K['statics'][key] illegal
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have some vague ideas about this:
- We can modify the
Schema
type so thatvirtual
(and others) will "concat" the type information and return the newSchema
type.- A similar implementation can be found here, where
@commander-js/extra-typings
allowscommand.option().option().option().option()
to be typed: https://github.com/commander-js/extra-typings/blob/e53229bf91e73df4a1b5b6be3ef39b1737397b63/index.d.ts#L938-L957
- A similar implementation can be found here, where
- With the new
Schema
typeModel
should be able toinfer
the methods.
We can do this in another PR.
Pull Request Test Coverage Report for Build 12721566187Details
💛 - Coveralls |
import Database from '../../dist/database'; | ||
import Model from '../../dist/model'; | ||
import Database from '../../src/database'; | ||
import Model from '../../src/model'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Conflicts with branch master's code, could you update them? |
now