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

Ability to raise type checking error using something like a typeerror type. #26055

Closed
4 tasks done
vilicvane opened this issue Jul 30, 2018 · 3 comments
Closed
4 tasks done
Labels
Duplicate An existing issue was already created

Comments

@vilicvane
Copy link

Search Terms

typeerror type, user-defined type checking

Suggestion

Conditional type brings a useful method for composing types, but there are many scenarios that conditional type works while losing type safety. It would be nice to have a special type like typeerror to notify the compiler of incorrect types:

let foo: typeerror;
    ^^^ error

let bar: {yo: string; ha: typeerror};
    ^^^ error

Examples

interface Foo {
  yo: string;
  ha: number;
  pia: boolean;
};

type Bar = {
  [K in keyof Foo]:
    Foo[K] extends string ? string[] :
    Foo[K] extends number ? number :
    typeerror;
};

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. new expression-level syntax)
@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Jul 30, 2018
@DanielRosenwasser
Copy link
Member

This has come up, but the biggest question is really just a matter of where and how to give the error when the type appears in an arbitrarily complex type.

@mattmccutchen
Copy link
Contributor

Looks like a duplicate of #23689.

@vilicvane
Copy link
Author

@mattmccutchen Thanks for referring.

@mhegazy mhegazy added Duplicate An existing issue was already created and removed Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript labels Jul 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants