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

Enhance how the Props type is used #366

Merged
merged 2 commits into from
Dec 12, 2023
Merged

Enhance how the Props type is used #366

merged 2 commits into from
Dec 12, 2023

Conversation

remcohaszing
Copy link
Member

Initial checklist

  • I read the support docs
  • I read the contributing guide
  • I agree to follow the code of conduct
  • I searched issues and couldn’t find anything (or linked relevant results below)
  • If applicable, I’ve added docs and tests

Description of changes

If the Props type isn’t defined, it now falls back to an empty shape instead of any. Also all keys are marked as readonly. As a result, props are no longer represented using the string Props, but an inline representation of the shape of the props.

Given that the upcoming support for an MDX layout also requires a patched version of Props, this new setup is more consistent.

Also the props description now contains a link to the props page on the MDX website.

If the Props type isn’t defined, it now falls back to an empty shape
instead of any. Also all keys are marked as readonly. As a result, props
are no longer represented using the string `Props`, but an inline
representation of the shape of the props.

Given that the upcoming support for an MDX layout also requires a
patched version of `Props`, this new setup is more consistent.

Also the `props` description now contains a link to the props page on
the MDX website.
@remcohaszing remcohaszing added 🦋 type/enhancement This is great to have 🗄 area/interface This affects the public interface 👶 semver/patch This is a backwards-compatible fix 🤞 phase/open Post is being triaged manually labels Dec 12, 2023
Copy link

changeset-bot bot commented Dec 12, 2023

🦋 Changeset detected

Latest commit: c7015d5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@mdx-js/language-service Patch
@mdx-js/language-server Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@remcohaszing remcohaszing marked this pull request as ready for review December 12, 2023 14:52
' */',
'export default function MDXContent(props) {',
' return <>{Math.PI}</>',
'}',
'',
'// @ts-ignore',
'/** @typedef {Props} MDXContentProps */',
'/** @typedef {0 extends 1 & Props ? {} : Props} MDXContentProps */',
Copy link
Member

Choose a reason for hiding this comment

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

{} is not an empty object type, it is an any type (minus null and undefined).
If you want to represent an empty object see https://github.com/sindresorhus/type-fest/blob/main/source/empty-object.d.ts

Copy link
Member Author

@remcohaszing remcohaszing Dec 12, 2023

Choose a reason for hiding this comment

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

{} is indeed not an object. It is an empty shape whose properties may be accessed, in other words, everything except null or undefined. Likewise { toString(): string } is not an object, but a shape that has a toString() method that returns string, in other words, pretty much everything except null and undefined.

To represent an actual object, the object type can be used, or object & { someProprty: string }. However, typically this doesn’t matter.

The goal here, and often with TypeScript, is not to ensure this is an empty object. It’s to ensure this is a shape whose properties may be accessed, even if it has no properties.

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’m going to merge this, because I really need this change. Feel free to open an issue if you strongly feel I’m wrong about this.

@remcohaszing remcohaszing merged commit ee4439a into main Dec 12, 2023
8 checks passed
@remcohaszing remcohaszing deleted the enhance-props-types branch December 12, 2023 20:35

This comment has been minimized.

@github-actions github-actions bot mentioned this pull request Dec 12, 2023
@remcohaszing remcohaszing added the 💪 phase/solved Post is done label Dec 12, 2023
@github-actions github-actions bot removed the 🤞 phase/open Post is being triaged manually label Dec 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🗄 area/interface This affects the public interface 💪 phase/solved Post is done 👶 semver/patch This is a backwards-compatible fix 🦋 type/enhancement This is great to have
Development

Successfully merging this pull request may close these issues.

2 participants