-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Issues with getting the typings correctly #26252
Comments
In a simplified test case: type MapFuncs<T> = { [K in keyof T]: Funcs<T[K]> };
type Funcs<T> = T extends () => infer R ? R : T;
declare function create<T>(options?: MapFuncs<T>): T;
create({
data() { return 1; },
dataReturn() { return this.data * 2; },
}); What do you expect the inferred type argument for the |
on the case of: create({
data() { return 1; },
dataReturn() { return this.data * 2; },
calculate(a: number, b: number){
return a+b;
}
});
// expect: { data: number, dataReturn: number, calculate: (a: number, b:number)=>number } A practical example of this is VueJs when using Other thing is the compiler doesn't throw error: create({
data() { return this.empty; }, // empty doesn't exist nor is defined
}); |
@pikax You probably don't have Based on what you're saying it sounds like you have the parameter and return type reversed? You also need to set type MapFuncs<T> = { [K in keyof T]: Funcs<T[K]> };
type Funcs<T> = T extends () => infer R ? R : T;
declare function create<T>(options?: T & ThisType<MapFuncs<T>>): MapFuncs<T>;
const x = create({
data() { return 1; },
dataReturn(): number { return this.data * 2; },
});
x.data * x.dataReturn; // works |
That seems to be work, thank you, is there any documentation for |
TypeScript Version: 3.1.0-dev.20180807
Search Terms:
Extend functions
Generic
Auto-complete
Template
Code
Expected behavior:
this.data
indataReturn()
should be interpreted asnumber
based on theFuncs<T>
type.return this.r;
should throw an error Cannot find name 'r'Actual behavior:
this.data
is interpreted different depending on the IDE (not sure if this is typescript compiler issue)this
is anythis
is resolve correctly but the props are all any making not as usefullFuncs<T>
return this.r
compiler doesn't complain about it, even if it doesn't exist on the type.Playground Link:
playground
Related Issues:
The text was updated successfully, but these errors were encountered: