I'm trying to write a type definition for a custom built module specification I've inherited here. Cannot figure it out. The trick is that the this context in the computed context in functions should be driven from properties such that shouldBeValueA is driven from keyA.
define.model("moduleName",
[
"jquery"
],
function($) {
return this.viewModel({
pub: {
properties: {
keyA: "valueA"
},
functions: {
keyB: this.computed(function() {
var shouldBeValueA = this.keyA;
})
}
}
})
})
Best definition I've got so far:
interface Define {
model: (
name: string,
dependencies: string[],
moduleContext: <T>(this: {
computed: (context: (this: T) => any) => KnockoutComputed<any>,
viewModel: (options: {
pub: {
properties: T,
functions: any
},
}) => any;
},
...args) => void) => void;
}
declare var define: Define;
But this errors: "Property keyA does not exist on type T"
this.viewModel()is preventing the typeTfrom being inferred "upward". I played with this in the Playground a bit and couldn't get it to work. EitherTis inferred as{}or it seems to be unresolved asT(as your current code does). Only workaround was to definemodel<T>instead ofmodelContext<T>, then call using a type likemodel<{ keyA: string }>...type viewModel<T> = (options: { pub: { properties: T, functions?: any }, }) => any interface Define { model: ( name: string, dependencies: string[], moduleContext: <A, F extends viewModel<A>>(this: { computed: (context: (this: A) => any) => KnockoutComputed<any>, viewModel: F }, ...args) => any) => any; }