-
Notifications
You must be signed in to change notification settings - Fork 8
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
.toMatchTypeOf()
does not take into account readonly
properties (undocumented case?)
#55
Comments
Discussed this elsewhere, but There's an outstanding, old feature request to rename But as mentioned there, since this library is already in use in a bunch of places, I want to get a v1 out before making that kind of change, or even introducing confusion with an alias. @mrazauskas it's very hard for me to remember what it's like not knowing this stuff, as I've now known it for years - it would be greatly appreciated if you could open a PR against the readme communicating any info that would have been helpful to you when you learned this the hard way recently. |
Looks like a better idea could be to advise using import { expectTypeOf } from 'expect-type';
expectTypeOf<{ readonly a: string; b: number }>().not.toEqualTypeOf<{ a: string; b: number }>();
expectTypeOf<Pick<{ readonly a: string; b: number }, "a">>().not.toEqualTypeOf<{ a: string }>(); (EDIT That’s just a note for future reader. Not an issue for me. Clicked wrong button.) |
By the way, it might be worth documenting this quirk of expectTypeOf<{ a: string; b: number }>().toMatchTypeOf<{ a: string | number | undefined }>(); Current documentation states: "To allow for extra properties, use |
Yeah, that's probably worth documenting better. Roughly, toMatchTypeOf is checking whether
|
What is All what I try to say: it is really hard to document the behaviour of |
expectTypeOf<X>().toMatchTypeOf<Y>()
If we wrote a expectTypeOf<never>().toExtend<string>() // ok
expectTypeOf<never>().toExtend<number>() // ok
expectTypeOf<never>().toExtend<{abc: boolean}>() // ok Because Having said all that, I take your point that it's somewhat confusing. I could potentially be open to a future version of this library which adds Re backwards compatibility, I agree, we've just released v1 but it was a deliberate choice to not include breaking changes. A lot of people were using v0 of this library already, so v1 was really just a "courtesy". Breaking changes could still arrive in v2. I'm going to reopen this and see if I can come up with a best-of-both-worlds solution. |
Regarding breaking changes. I was trying to point out that if someone used |
Opened #126, you can try it out with Usage: type MyType = {readonly a: string; b: number; c: {some: {very: {complex: 'type'}}}; d: 'another type'}
expectTypeOf<MyType>().toMatchObjectType<{a: string; b: number}>() // fails - forgot readonly
expectTypeOf<MyType>().toMatchObjectType<{readonly a: string; b?: number}>() // passes @mrazauskas fair point re |
By the way, does |
I don't think so. There is the "Open in Stackblitz" link which lets you play around though, but you need to add a tsconfig and .ts file manually for a library like this (CC @Aslemammad would be nice if the template included typescript, a basic tsconfig with strict/skipLibCheck, and an index.ts file? Lots of libraries like expect-type, arktype, trpc, zod etc. would benefit from that) |
Yes, it'd be nice, but that's a default option we thought of removing before so it'd just motivate users to have their own templates. The reason we have it is pure motivation 😅 so I recommend shipping your own template with that config. |
From #52 (comment)
The documentation suggests that
.toMatchTypeOf()
can be used to "allow for extra properties". Just wanted to draw attention that it might need better description and/or examples.Currently at least for me it sounds like
.toMatchTypeOf()
and.toEqualTypeOf()
are identical, but.toMatchTypeOf()
allows testing partial match. That seems to be incorrect, whenreadonly
gets involved:playground
The text was updated successfully, but these errors were encountered: