microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
98.89k stars 12.28k forks source link

Discriminated union with an optional discriminator is not being resolved as expected #58508

Open vladmw5 opened 1 month ago

vladmw5 commented 1 month ago

🔎 Search Terms

discriminated union, discriminated union with an optional discriminator, type inference in objects

🕗 Version & Regression Information

I have a simple piece of code (see the playground/below)

The runtime flow is very simple: whatever gets returned by the loader func gets passed as data to the handle's crumbBuilder method. If the loader is absent then data will also be absent

But when it comes to typing those behaviors then 2 issues arise:

1) At first, the discriminated union is not being resolved properly: in the second case it should be a string but TS cannot infer it and defaults to any 2) At second, we still have to explicitly annotate or assert the data type, however from the loader definition it is possible to infer its return type. Is there a syntax way to say 'the type of data is the whatever awaited type the loader function returns?. I have tried to achieve that behaviour using generics and thesatisifies` keyword but failed to

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBACghsAFgZQgcwLYQHbCgXigHsAjAKwgGNgBtAXQG4BYAKFdEigAk5sATADYQAPABUAIgjgA+AlADerKMqiUATgFcMJAEIaAlgL4Q1ALigAKPlPMSpASgKz4SVJhy1GrAL6t24aAAZIjhjNTFJYBk5Czg1NABnc3koMDi4DCSoACUqIjU+YQTgNX1sNAAaKGLS8tlvR3xnNSIMfQSROyjpZjYWDmhsog1gCB5+IXCuuDleEFl8JRUAHwUllRVEXkEIc3Gd4WwIADcTHvWNqAEQsIB+cyPTtV6N3xYN1cV3y+Utid3uNshBEpOdvj9rqETOZglCppEZBdvL1WJQiNhilAWiMxkCTAB1fRIYbAWFhORfDZ-HbJC4bdRaXQGIzQyzWKKNWT0KoAeh57Jm7VU+TUVGAAhAUDKADMTKK+FA4AkoI8TEifNUEO1pfoIMqhjj9pM-Cw0Ri8NjRkaTBSLpCwuYlSBsJRLHFEpy1uDLqLgBo1NhFfEEgA6NJqDKhjQdNQAST4LxU3gqF2pQlp3vpmm0ekMDrZDicUHoUD5AqlyuwRDwMrlEAVSuqJTKaHVLG8muA2t1+pJuP+4RqLekQA

💻 Code

type PathSegment = object[];

type Handle<TData> = {
    crumbBuilder: (data: TData) => PathSegment[];
}

type Loader<TData> = (args: { params: Record<string, string> }) => Promise<TData>;

type RouteHandler<TData = any> =
    | {
        handle: Handle<never>;
        loader?: never;
    }
    | {
        handle: Handle<TData>;
        loader: Loader<TData>
    };

const routeHandlerWithoutLoader = {
    handle: {
        crumbBuilder: (data) => [], //data is correctly inferred as never
    }
} satisfies RouteHandler

const routeHandler = {
    loader: async (args) => {
        return args.params.userId;
    },
    handle: {
        crumbBuilder: (data) => [] //data is not inferred as string
    }
} satisfies RouteHandler<string>

🙁 Actual behavior

data is not inferred as string

🙂 Expected behavior

1) data should be inferred as a string

2) there is no need to explicitly annotate or assert the data type , and it must be inferred from the loader return type

Additional information about the issue

No response

vladmw5 commented 1 month ago

Here is the link to the discussion of the same issue on the TS github server:

https://discord.com/channels/508357248330760243/1239320604713287830

Andarist commented 1 month ago

According to discriminateContextualTypeByObjectMembers the function expression used for the loader isn't possibly a discriminant value. So it's not discriminated based on that. When it comes to the optional property in the contextual type... the type is discriminated based on that only when your node doesn't have that property - but you have the loader property.

RyanCavanaugh commented 2 weeks ago

I'd be curious if we could use never / undefined as possible discriminants here. Would be interested to experiment