-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Discriminated union(?) not working when fields have same name? #37343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This isn't a discriminated union because there's no discriminant. This pattern isn't very easy to model because it's susceptible to aliasing effects because you're counting on the absence of the |
Aaah, so that's what makes it a discriminated union. I figured it was was just using
So if I understand you correctly, to use discriminating unions, there has to be a common property that is always present and accountable? What do you mean with "guarantee that the field isn't present"? In my example, the intention seems clear to me, and in my newb mind typescript would force me into using the right combination? But is the issue when objects are coming "from the outside", so typescript doesn't have control over them? |
For example, this is legal code (according to TS) that will certainly cause a crash in the presence of an actual implementation of const obj = {
header: "Won't work",
render: (arg: Data) => arg.name.toLowerCase(),
field: "id"
};
const objRef: Header & ValueFromRow<Data> = obj;
makeTable(data, [objRef]); |
TypeScript Version:
3.8.3
Search Terms:
I haven't really searched because I don't know what words to use for this. 😕
Expected behavior:
No Typescript errors or warnings when calling
makeTable
.Actual behavior:
Following Typescript errors and warnings:
rows
gets typestring[]
, rather thanData[]
, for some reasonrow
in the render function ends up as implicitlyany
regardless.Typescript gets everything right if I rename one of the
render
properties to something else, but shouldn't a discriminated union(?) work regardless of what name I use?Plan was to use a
if( 'field' in column )
type-guard, which would then tell me which parameters to callrender
with, but Typescript started complaining before I even got there. 😕Code
Output
Compiler Options
Playground Link: Provided
The text was updated successfully, but these errors were encountered: