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

Add tests for Json and Hex type #42

Merged
merged 4 commits into from Oct 6, 2022
Merged

Add tests for Json and Hex type #42

merged 4 commits into from Oct 6, 2022

Conversation

Mrtenz
Copy link
Member

@Mrtenz Mrtenz commented Oct 6, 2022

Closes #40.

The test is almost completely copied from @metamask/types, with just one small adjustment. The following test would result in a compile-time type error:

// The Json type is too accepting of types. This type isn't valid Json because
// it can contain `undefined`, but the Json type allows it.
type TypeWithOptionalProperty = {
  a?: number;
};
const typeWithOptionalProperty: TypeWithOptionalProperty = { a: undefined };
expectAssignable<Json>(typeWithOptionalProperty);
TS2375: Type '{ a: undefined; }' is not assignable to type 'TypeWithOptionalProperty' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.

I added undefined to a's type, which makes it not assignable to Json, which seems to be the behaviour we want.

@Mrtenz Mrtenz requested a review from a team as a code owner October 6, 2022 13:00
Comment on lines 1 to 4
{
"name": "test-d",
"private": true
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tsd seems to be built for declaration-only packages, as it looks at the types field in package.json (i.e., dist/index.d.ts, with no way to disable that behaviour it seems. For that reason, I moved it into a separate folder and added a dummy package.json.

Copy link
Contributor

@mcmire mcmire Oct 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sounds like, judging from the README, that if you pass a path to tsd, then it won't look in the types field. Is this not true in your experience? Do you think that people will try to run tsd on its own instead of yarn test:types?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't able to get it to work using --typings or by passing a path, but apparently the behaviour is different if you specify a directory in the config. Seems to work fine now with the test being in the src folder.

@mcmire
Copy link
Contributor

mcmire commented Oct 6, 2022

@Gudahtt Pinging you on this PR since you added these types originally!

@Mrtenz Mrtenz changed the title Add tests for Json type Add tests for Json and Hex type Oct 6, 2022
@Mrtenz
Copy link
Member Author

Mrtenz commented Oct 6, 2022

Simplified the tsd setup and moved the Hex type tests from the @metamask/types package as well, so now all the type tests are moved.

Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, but I want to make sure Mark sees this.

Copy link
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

The test that was removed was rendered unnecessary by the exactOptionalPropertyTypes TypeScript option. An optional property can no longer be undefined.

@Mrtenz Mrtenz merged commit 4136519 into main Oct 6, 2022
@Mrtenz Mrtenz deleted the mrtenz/test-json-type branch October 6, 2022 19:51
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

Successfully merging this pull request may close these issues.

Copy type tests for Json from @metamask/types
3 participants