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

Expect Type Error #31

Open
anodynos opened this issue Mar 9, 2024 · 5 comments
Open

Expect Type Error #31

anodynos opened this issue Mar 9, 2024 · 5 comments

Comments

@anodynos
Copy link

anodynos commented Mar 9, 2024

Hey and thanks for this lib - I started using it to day, coming from using plain .ts files with typed variable assignments & forced errors with // @ts-expect-error on top. It worked well, but I want to get to something more structured.

I see I also have to use // ts-expect-error to catch expected errors in ts-expect-error, which is fine.

I'm also in the habit of documenting the expected errors, like we do in out code tests, but it's purely for documentation & future reference, as it's not possible to be checked. For example:

const identity = <T>(val: T): T => val

// @ts-expect-error: TS2322: Type number is not assignable to type string
const str: string = identity(123)

In ts-expect we have the expectType<number>("test"); // Compiler error example, but we don't document the error somehow, so it can be checked.

An exotic new version could support something like:

expectTypeError<number>("test", `TS2345: Argument of type string is not assignable to parameter of type number`);

and perhaps do some magic to obtain error codes & descriptions.

I know this is much much harder, and probably not just a lib, as it perhaps would have to incorporate information from the IDE or TS compiler to do the trick, but there might be a way ;-) Maybe an IDE plugin? Who knows!

Anyway, just a thought, just sharing!

@anodynos
Copy link
Author

Having used it for a whole day, I want to add another issue.

I find the strict version of

  expectType<TypeEqual<typeof value, SomeType>>(true)

much more useful than simple expectType, but when it fails during development it doesn't show the real error, instead it only shows TS2345: Argument of type true is not assignable to parameter of type false.

I resort to assignment to a variable, which is not always desirable... I think if this is fixed in will make the library 5x more useful!

@blakeembrey
Copy link
Member

Definitely agree with both of these, and I'll keep them in mind if anyone else wants to work on it or I find time, but I think they both require hooking into the TypeScript compiler directly.

Another option might be: https://github.com/tsdjs/tsd

It's much more complete and uses the compiler IIRC, so probably covers these cases already.

@mrazauskas
Copy link

@anodynos Please take a look at TSTyche (this my project), https://github.com/tstyche/tstyche. It has the .toRaiseError(), which is able to capture type error:

// matching message
expect<Matchers>().type.toRaiseError("requires between 1 and 2 type arguments");
// or code
expect<Matchers>().type.toRaiseError(2707);

By the way, a better idea to test assignably would be using .not.toBeAssignableTo().

@anodynos
Copy link
Author

@anodynos Please take a look at TSTyche (this my project), https://github.com/tstyche/tstyche. It has the .toRaiseError(), which is able to capture type error:

// matching message
expect<Matchers>().type.toRaiseError("requires between 1 and 2 type arguments");
// or code
expect<Matchers>().type.toRaiseError(2707);

By the way, a better idea to test assignably would be using .not.toBeAssignableTo().

Thank you @mrazauskas, it looks interesting, I will have a look at it when I have time for my projects - for now I'm heavily using expectType and the gang, eg https://github.com/NeoZen-dev/neozen-tools/blob/78604a935ec95357db2f65c5e115893883a67ccf/packages/zen/src/code/loopzen/__tests__/all-typings/master/Master-typings-spec.masterTemplate.ts#L307

@mrazauskas
Copy link

mrazauskas commented Nov 24, 2024

@anodynos Thanks for sharing your project!

To add few more details, I was developing TSTyche to be able to test types of a project that had more than 1000 assertions (in a monorepo with more than 20 packages). At this scale I wanted to be able:

  • to run a single test file,
  • to have .only and .skip,
  • to make sure the lowest supported TypeScript version is ...

TSTyche can do all this (and more!). At any scale (;


It works with any generic type assertion library as well. You can use TSTyche with ts-expect, for example. But beware that TypeEqual of ts-expect does not take into account TypeScript compiler’s options like exactOptionalPropertyTypes, playground:

import { expectType, TypeEqual } from "ts-expect";

expectType<TypeEqual<{ a?: number | undefined }, { a?: number }>>(true); // pass? really?
expectType<TypeEqual<{ b?: number }, { b?: number | undefined }>>(true); // pass.. no way!!

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

3 participants