'Can someone explain how does Typescript inferred the type of Generic T here?
How did typescript inferred that: T will be "basic" | "minimal" instead of the entire object passed. Can someone please explain how is this possible? How does typescript infer types?
type TProjectionKeyValue = { [key: string]: 0 | 1 };
type PType = "basic" | "minimal";
const ProjectionBuilder = <T extends PType>(projections: { [key in T]: TProjectionKeyValue }, defaultProjection: string = "basic") => {
const buildProjection = (requiredProjections: T[]) => {
};
return buildProjection;
};
const mainProjections = ProjectionBuilder({
basic: {
name: 0,
kdldl: 1,
jkjk: 0
},
minimal: {}
});
const x = {
basic: {
name: 0,
kdldl: 1,
jkjk: 0
},
minimal: {}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
