-
Notifications
You must be signed in to change notification settings - Fork 25
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
improved generics #242
improved generics #242
Conversation
@sisp could you please review it? |
Codecov Report
@@ Coverage Diff @@
## master #242 +/- ##
==========================================
+ Coverage 90.42% 90.47% +0.05%
==========================================
Files 170 170
Lines 4875 4892 +17
Branches 761 765 +4
==========================================
+ Hits 4408 4426 +18
+ Misses 450 449 -1
Partials 17 17
Continue to review full report at Codecov.
|
Looks great in my opinion! The only thing I noticed is that the generic types must be provided explicitly when instantiating a model, they aren't inferred. @model("GenericModel")
class GenericModel<T1, T2> extends Model(<U1, U2>() => ({
v1: prop<U1>(),
v2: prop<U2>(),
v3: prop<number>(0),
}))<T1, T2> {}
const s = new GenericModel({ v1: "1", v2: 2, v3: 3 }) // GenericModel<unknown, unknown> I'm not sure if it's possible to get those generics inferred though. |
No idea, if you find a way let me know. It seems you've really looked into it :) |
Thank you very much for your efforts on improving generic models! 🙏 This is a great improvement in my opinion. If I find a way to have the generics inferred, I'll let you know. |
Please do, I'm really curious :) |
@xaviergonz It seems the conditional export type MaybeOptionalModelProp<TPropValue> = ModelProp<
TPropValue,
TPropValue,
- IsOptionalValue<TPropValue, string, never>
+ string
> then inferring the generics works: @model("GenericModel")
class GenericModel<T1, T2> extends Model(<U1, U2>() => ({
v1: prop<U1>(),
v2: prop<U2>(),
v3: prop<number>(0),
}))<T1, T2> {}
const s = new GenericModel({ v1: "1", v2: 2, v3: 3 })
assert(s, _ as GenericModel<string, number>) // OK
@model("ExtendedGenericModel")
class ExtendedGenericModel<T1, T2> extends ExtendedModel(<T1, T2>() => ({
baseModel: modelClass<GenericModel<T1, T2>>(GenericModel),
props: {
v4: prop<T2>(),
},
}))<T1, T2> {}
const e = new ExtendedGenericModel({ v1: "1", v2: 2, v3: 3, v4: 4 })
assert(e, _ as ExtendedGenericModel<string, number>) // OK I'm just not sure yet how to fix it. If you have an ingenious idea, I'm all ears. 😄 |
Thanks for the pointers. Generics should now be inferable in v0.58.2 :D |
Incredible, I don't know how long it would have taken me to find that solution! Thank you very much (again), you're a genius! 😃 |
My only skill is to be able to throw random attempts to fix it at the wall for long enough until one works 😉 |
random -> educated guessing I'd say 😉. |
No description provided.