说我有一堂课:
export class TypeCreator {
entitiesMap: Map<string, any>;
registerEntities(namespace: string, e: any): any {
this.entitiesMap.set(namespace,e);
return e;
}
}
我想做的就是返回e的类型,像这样:
registerEntities<T>(namespace: string, e: T): T {
this.entitiesMap.set(namespace,e);
return e;
}
但是问题是,registerEntities可以用这样的匿名对象调用:
const entities = tc.registerEntities('foo', {bar: true, baz: 'samsonite'});
所以我不能做类似的事情:
const entities = tc.registerEntities<typeof anonObject>('foo', {bar: true, baz: 'samsonite'});
有人知道怎么做吗?