We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Inference, ThisType, this, getter
Playground link with relevant code
function myfunc<T>( options: T & ThisType<T & { name: string }> ): T & { name: string } { return options as any; } const A = myfunc({ get nickname() { const name = this.name; return name; }, getNickname() { const name = this.name; return name; }, });
this.name is string:
this.name
but const name is any:
const name
expect const name infer to string type, the same as this.name.
The text was updated successfully, but these errors were encountered:
Similar issue with ThisType and generic constraints:
ThisType
Sometimes it's and sometimes it's
If Record<string, PropertyDefinition> constraint of generic param T of function model is removed, it works as expected
Record<string, PropertyDefinition>
T
model
type TypeMap<T> = StringConstructor extends T ? string : NumberConstructor extends T ? number : T; type PropertyDefinition = { type?: StringConstructor | NumberConstructor; required?: true; computed?(): unknown; }; type Expanded<T> = T extends infer U ? { [P in keyof U]: U[P] } : never; type DeriveType<T> = T extends { type: infer U } ? TypeMap<U> | (T extends { required: true } ? never : undefined) : T extends { computed: () => infer U } ? U : never; type Entity<T> = Expanded<{ [K in keyof T]: DeriveType<T[K]>; }>; function model<T extends Record<string, PropertyDefinition>>( definition: T & ThisType<Entity<T>> ): T { return definition; } const BillItem = model({ price: { type: Number, required: true }, amount: { type: Number, required: true }, description: { type: String }, total: { computed() { return this.amount * this.price; }, }, });
Sorry, something went wrong.
No branches or pull requests
Bug Report
π Search Terms
Inference, ThisType, this, getter
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
this.name
is string:but
const name
is any:π Expected behavior
expect
const name
infer to string type, the same asthis.name
.The text was updated successfully, but these errors were encountered: