Skip to content
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

add support throw keyword #40768

Closed
5 tasks done
sahandevs opened this issue Sep 25, 2020 · 1 comment
Closed
5 tasks done

add support throw keyword #40768

sahandevs opened this issue Sep 25, 2020 · 1 comment

Comments

@sahandevs
Copy link

Search Terms

Error Type, throws, throw, throw keyword

Suggestion

Explanation

add a throw keyword that when it reached in a type it reports a message/error.
it's useful when used with "Template literal types".

usage:

type RouteParams<T extends string> =
  T extends `/${infer Route}/:${infer Param}`
    ? { [Route]: { [Param]: string }}
    : throws `${T} doesn't have a "/:param" part`;

function addRoute<T extends string>(path: T, params: RouteParams<T>) ...

for now, we can only return types like this to inform the user why the input is incorrect:

type Error<T> = T & { error: true };

type RouteParams<T extends string> =
  T extends `/${infer Route}/:${infer Param}`
    ? { [Route]: { [Param]: string }}
    : Error<`${T} doesn't have a "/:param" part`>;

function addRoute<T extends string>(path: T, params: RouteParams<T>) ...

which doesn't show error lines and place of the error message is really indirect and the user may not found it/

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@MartinJohns
Copy link
Contributor

Duplicate of #23689.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants