How to properly define a recursive type in TypeScript

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):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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?

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật