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

Checking assignability without losing inferred types and without runtime code #26045

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

Comments

@wkrueger
Copy link

Search Terms

type erase assert validat assign

Suggestion

Recently I've been using a construct like the following in order to validate structures against a determinate type while still keeping their inferred type:

const validateType = <Requirement>() => <T extends Requirement>(i: T) => i
const aFunctionRecord = validateType<Record<string, Function>>() => ({
  hello: (world: string) => { ... },
  typescript: (cool: number) => { ... }
})
//aFunctionRecord retains its inferred type and will error if does not confirm the requirement

Oh, and this sometimes even infers parameters for functions, saving the need for some type annotations.

This is different from writing something like

const anotherFunctionRecord: Record<string, Function> = { ... }
//the inferred type is "lost"

While validateType works great, it feels a bit hacky, since we need to create a "concrete" JS function in order to achieve that. It would be great if that type of check could be achieved just with type annotations. Example:

const aFunctionRecord : * extends Record<string, Function> = { ... }
//or
const aFunctionRecord = { ... } extends Record<string, Function>

I have used extends in the example here since it's an already existing keyword, if you come up with a better idea...

thanks!

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)
@vilicvane
Copy link
Contributor

Possibly duplicate of #16605?

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Jul 30, 2018
@wkrueger
Copy link
Author

wkrueger commented Jul 31, 2018

Hmm got quite confused on comparing both issues. But they seem slightly different.

This issue (26045) seems well described by Daniel's comment on that issue #16605 (comment).

On the other hand, the 1st post of #16605 seems to ask something slightly different (related to the different strictness levels if you pass either a variable or a literal to a function).

@RyanCavanaugh RyanCavanaugh added Duplicate An existing issue was already created and removed Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Dec 16, 2021
@RyanCavanaugh
Copy link
Member

Tracking at #7481

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

3 participants