The following is an issue I’ve been battling with for about four days now. I want to define function types where the functions form part of a group such that the group contains pointers to the functions (and potentially other values) based on some parameter. Playground Link
The following is a basic representation of what I’m trying to achieve (view in playground link above):
<code>type ValueGroup = Record<string, unknown>
type ValueGroupCreationCallback = (...args: any[]) => ValueGroup
export class ValueGroupFactory<
CreationCallback extends ValueGroupCreationCallback
creationCallback: CreationCallback
constructor(creationCallback: CreationCallback) {
this.creationCallback = creationCallback
createGroup(...params: Parameters<CreationCallback>) {
return this.creationCallback.call(
) as ReturnType<CreationCallback>
function member1(g: ReturnType<typeof factory.createGroup>, string: string) { // Error
function member2(g: ReturnType<typeof factory.createGroup>, number: number) { // Error
const factory = new ValueGroupFactory((name: string) => { // Error
get member1() { // Error: 'member1' implicitly has return type 'any' because it does not have a return type annotation...
return member1.bind(group)
get member2() { // Error: 'member2' implicitly has return type 'any' because it does not have a return type annotation...
return member2.bind(group)
const group1 = factory.createGroup("John") // Has type `any` instead of { person, member1, ... }
<code>type ValueGroup = Record<string, unknown>
type ValueGroupCreationCallback = (...args: any[]) => ValueGroup
export class ValueGroupFactory<
CreationCallback extends ValueGroupCreationCallback
> {
creationCallback: CreationCallback
constructor(creationCallback: CreationCallback) {
this.creationCallback = creationCallback
}
createGroup(...params: Parameters<CreationCallback>) {
return this.creationCallback.call(
null,
...params
) as ReturnType<CreationCallback>
}
}
function member1(g: ReturnType<typeof factory.createGroup>, string: string) { // Error
return [string, g]
}
function member2(g: ReturnType<typeof factory.createGroup>, number: number) { // Error
return [number, g]
}
const factory = new ValueGroupFactory((name: string) => { // Error
const group = {
person: { name },
get member1() { // Error: 'member1' implicitly has return type 'any' because it does not have a return type annotation...
return member1.bind(group)
},
get member2() { // Error: 'member2' implicitly has return type 'any' because it does not have a return type annotation...
return member2.bind(group)
}
}
return group
})
const group1 = factory.createGroup("John") // Has type `any` instead of { person, member1, ... }
</code>
type ValueGroup = Record<string, unknown>
type ValueGroupCreationCallback = (...args: any[]) => ValueGroup
export class ValueGroupFactory<
CreationCallback extends ValueGroupCreationCallback
> {
creationCallback: CreationCallback
constructor(creationCallback: CreationCallback) {
this.creationCallback = creationCallback
}
createGroup(...params: Parameters<CreationCallback>) {
return this.creationCallback.call(
null,
...params
) as ReturnType<CreationCallback>
}
}
function member1(g: ReturnType<typeof factory.createGroup>, string: string) { // Error
return [string, g]
}
function member2(g: ReturnType<typeof factory.createGroup>, number: number) { // Error
return [number, g]
}
const factory = new ValueGroupFactory((name: string) => { // Error
const group = {
person: { name },
get member1() { // Error: 'member1' implicitly has return type 'any' because it does not have a return type annotation...
return member1.bind(group)
},
get member2() { // Error: 'member2' implicitly has return type 'any' because it does not have a return type annotation...
return member2.bind(group)
}
}
return group
})
const group1 = factory.createGroup("John") // Has type `any` instead of { person, member1, ... }
The goal is to make the functions callable through their group by passing the exact parameters they require for their specific operations (through binding) and not other values that might not be primary to what they do. I’ve tried several approaches to no avail. TS just assigns the values the type any
. I would like to get rid of the getters too if possible. How am I supposed to be implementing this?