Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Conversation

D-Sketon
Copy link
Member

@D-Sketon D-Sketon commented Jun 30, 2024

now

this.findById(id, { lean: true }); // <-- T
this.findById(id, { lean: false }); // <-- Document<T>
this.findById(id, {}); // <-- Document<T>
this.findById(id, {} as Partial<Options>); // <-- Document<T> | T

@coveralls
Copy link

coveralls commented Jun 30, 2024

Pull Request Test Coverage Report for Build 9732452201

Details

  • 123 of 123 (100.0%) changed or added relevant lines in 7 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.02%) to 98.295%

Totals Coverage Status
Change from base Build 9700112432: 0.02%
Covered Lines: 4438
Relevant Lines: 4515

💛 - Coveralls

@coveralls
Copy link

coveralls commented Jul 4, 2024

Pull Request Test Coverage Report for Build 9797301066

Details

  • 140 of 140 (100.0%) changed or added relevant lines in 7 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.02%) to 98.295%

Totals Coverage Status
Change from base Build 9700112432: 0.02%
Covered Lines: 4438
Relevant Lines: 4515

💛 - Coveralls

@D-Sketon D-Sketon marked this pull request as ready for review July 6, 2024 07:32
@coveralls
Copy link

coveralls commented Jul 6, 2024

Pull Request Test Coverage Report for Build 9817607131

Details

  • 185 of 185 (100.0%) changed or added relevant lines in 7 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.01%) to 98.289%

Totals Coverage Status
Change from base Build 9700112432: 0.01%
Covered Lines: 4424
Relevant Lines: 4501

💛 - Coveralls

SukkaW
SukkaW previously approved these changes Jul 6, 2024
@coveralls
Copy link

coveralls commented Jul 6, 2024

Pull Request Test Coverage Report for Build 9819663515

Details

  • 186 of 186 (100.0%) changed or added relevant lines in 7 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.02%) to 98.29%

Totals Coverage Status
Change from base Build 9700112432: 0.02%
Covered Lines: 4425
Relevant Lines: 4502

💛 - 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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warehouse/src/model.ts

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?

Copy link
Member Author

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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants