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

Testing the value of types #290

Open
ifiokjr opened this issue Nov 8, 2018 · 2 comments
Open

Testing the value of types #290

ifiokjr opened this issue Nov 8, 2018 · 2 comments
Labels

Comments

@ifiokjr
Copy link

ifiokjr commented Nov 8, 2018

Thanks for another great library! It really makes it easy to combine the validation of types with my current jest setup.

There's one question I have. In my tests, I need to check that my type inference is working correctly for an instance of a class I created. In my DTSLint tests, I created several something similar to the following.

// $ExpectType "extraArg" | "extraArgs" | "simple" | "withReturnValue"
type A = keyof typeof model.methods;

Is it currently possible to port over the above tests from DTSLint? Does this library provide a way of testing the exact value of types?

@ikatyang
Copy link
Owner

We currently do not support exact type testing and I think it may be hard to use since the type display is not stable (microsoft/TypeScript#17944).

If all of your tests are passed in dtslint, it should be fine to just replace those $ExpectType with @dts-jest:snap and run the tests. If you'd like to check the correctness, you could generate diff-friendly snapshots and replace @dts-jest:snap -> with $ExpectType in snapshots, the output should be exact the same as your dtslint test files.

@timkinnane
Copy link

Thanks very much for this package, I haven't found anything like it before and I really needed it.

I would also like to make type assertions. I'm using dts-jest-remap now as suggested above for easier manual checks, but it still feels a bit clunky. It doesn't allow for TDD with utility types because you have to generate snapshots first and check the outputs until you have what you intended. I'd really love to assert types up front, then work on the type until it passes. The syntax could work similar to how the diff snap is generated:

// @dts-jest -> { a: any }
type x = Required<{ a?: any }>

I get that type display may not be stable from one TS version to the next, but for my project I'd be wiling to maintain that as it changes and it might even help me to surface those changes when I update TS.

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

No branches or pull requests

3 participants