-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type-level narrowing constraints (like as const
)
#53813
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
You can already do this: function A<T>(val: T) {}
function B<const T>(val: T) {}
A(123) // Typed number
B(123) // Typed 123 https://devblogs.microsoft.com/typescript/announcing-typescript-5-0/#const-type-parameters |
thanks for the quick reply @MartinJohns! I'm not clear how this solves the use case: here's a playground link. Can you update the library code such that In your example you are typing an argument, but it doesn't seem to work so well if you type the return value: |
A |
@dimitropoulos Reusing your example from above, here's how to narrow the type. type Narrowable = string | number | bigint | boolean;
const UploadThingServerHelper = <ValidRoutes extends Record<string, Narrowable>>(
route: {
readonly [Route in keyof ValidRoutes]: {
middleware: () => ValidRoutes[Route],
onUpload: (response: { metadata: ValidRoutes[Route] }) => void;
};
}
) => {};
middleware: () => F.Narrow<ValidRoutes[Route]>, |
I accidentally got pointed here by @jcalz's comment here and it turns out that my PR from yesterday fixes the problem mentioned here. @dimitropoulos, could you check if that covers the whole thing you reported here? |
@Andarist I didn't run it myself, but it certainly seems like it would solve the problem! |
Suggestion
In userland code, you can use
as const
, but as a library author, I often want to provide the best possible experience, and when there's an opportunity for a literal value to be produced, I would like to infer that value for my users without them having to use (or remember to use)as const
.🔍 Search Terms
constant literals, Math.random, require as const, infer literals, readonly by default
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
Provide an ability to narrow types at the type level (i.e. without JavaScript where you can use
as const
).📃 Motivating Example
A type like this produces
() => string
. I can improve the situation by specifyingas const
, which will cause it to produce() => "A"
.However, in the context of a library, a library author doesn't have the option to do this in the type level. Something for this use-case would be a nice improvement:
💻 Use Cases
This is useful for libraries trying to provide a great user experience with great inferencing. It should be a backwards compatible change. There are no workarounds that I'm aware of (from the code of a library).
The text was updated successfully, but these errors were encountered: