























这是一个创建于 359 天前的主题,其中的信息可能已经有所发展或是发生改变。
class a extends schema {
fun1 () => 1
}
class b extends schema{
fun2 () => 2
}
const objs = {
a:new a
b:new b
}
function find(key) : any{
return objs[key]
}
这个 objs 很多个 obj 他们都继承自同一个父类,有什么办法在调用 find 返回的对象能保持代码提示吗?
let obj = find('a')
obj.fun1 //代码提示
第 1 条附言 · 2025 年 6 月 20 日
class Schema{
protected json: Record<string, any> = {}
attr(key:string,value:any){
this.json[key] = value
}
find(id:string){
return finder(this.json, id)
}
}
function finder(obj: Record<string, any>, id: string) {
// 递归查找 id 对应的 obj
// obj 都是继承自 Schema 的
return obj
}
let a = new Schema
a.find('b')
json 里的内容是动态,似乎无法实现,试了好几个AI改出来的都不对。
1 RomeoHong 2025 年 6 月 20 日type Objs = typeof objs; |
3 bjzhou1990 2025 年 6 月 20 日function find<T extends Schema>(key: string): T { const c = find('a') 不知道是不是你要的 |
5 nebnyp410404 2025 年 6 月 21 日class schema { class a extends schema { const objs = { type Key = keyof typeof objs; function find<T extends Key>(key: T): typeof objs[T] { const cc = find('a') |
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。