It is difficult to articulate exactly what I want to achieve, so I will post the code. I have a class that should fetch config that should dynamically return the correct config type based on the id specified in the contractor. Question: How should I structure my code solve my problem spesified below?
export type ConfigA = {
id: "configA";
name: string;
propA: string;
};
export type ConfigB = {
id: "configB";
name: string;
propB: string;
};
export type Configs = ConfigA | ConfigB;
export class ConfigService<C extends Configs> {
private readonly configCollection: CollectionReference<C>;
constructor(private configName: C["id"]) {
this.configCollection = db.collection("config") as CollectionReference<C>;
}
public async get() {
const configSnap = await this.configCollection.doc(this.configName).get();
return configSnap.data();
}
}
Class callee
const configA = await new ConfigService("configA").get();
const propA = configA.propA;
The Problem:
Property 'propA' does not exist on type 'Configs'.
Property 'propA' does not exist on type 'ConfigB'
