The title of my question may not have been clear about what I am looking for, but what I need is something known as discriminated unions. You can find more information about it here: https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#discriminating-unio...>
I have a type A with two fields, B and C. Field B has a well-defined set of string literals, and I want field C's type to be dependent on the value of field B.
Here is some code to clarify:
type Field1Values = "a" | "b" | "c";
type TypeForValueA = {
...
}
type TypeForValueB = {
...
}
type TypeForValueC = {
...
}
type RootType = {
field1: Field1Values,
field2: <something here that I didn't know exactly what to put>
}
const objA: RootType = {
field1: "a",
field2: {
// Only the values from the type TypeForValueA are allowed
}
}
const objB: RootType = {
field1: "b",
field2: {
// Only the values from the type TypeForValueB are allowed
}
}
How can I achieve the desired effect?
Thank you 😃
Edit:
I have already tried following the instructions in the documentation example:
type NetworkState =
| NetworkLoadingState
| NetworkFailedState
| NetworkSuccessState;
However, all the fields from all types are being displayed in the lint results. I only want the fields from the correct type to be shown.