I have this map function that is rendering the type of an object.
function mapStuffs(fields: any) {
switch (fields.type) {
case "NewStuffs":
return {
type: "NewStuffs" as const,
stuff: fields.stuff
};
case "StuffFile":
const fileName = "jadflkjs";
const stuffId = "adlkajsf";
return {
type: "StuffFile" as const,
fileName,
stuffId
};
default:
return {
type: "ExistingStuff" as const
};
}
}
And when I build, the generated type is:
mainstuff:
| {
stuff: string | undefined;
type: "NewStuffs";
fileName?: undefined;
stuffId?: undefined;
}
| {
type: "StuffFile";
fileName: string;
stuffId: string;
}
| {
type: "ExistingStuff";
fileName?: undefined;
stuffId?: undefined;
};
I don't understand how to remove fileName and stuffId as optional parameters when they're not required. Is it a typescript bug? What I would like to have is this type:
mainstuff:
| {
stuff: string | undefined;
type: "NewStuffs";
}
| {
type: "StuffFile";
fileName: string;
stuffId: string;
}
| {
type: "ExistingStuff";
};