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

docs: global type recommendation #6676

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

privatenumber
Copy link

@privatenumber privatenumber commented Oct 10, 2024

Description

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
    Sorry, no prior Issue.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

Problem

Currently, users are encouraged to use the types property in tsconfig.json, which disables type auto detection.

This can be dangerous as if their project relies on it, it will no longer auto import types and break it. And even if not, it sets a precedent for users to manually manage types from packages that would otherwise be auto imported.

Changes

Recommend importing vitest/globals from a TS file

Copy link

netlify bot commented Oct 10, 2024

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 67dce54
🔍 Latest deploy log https://app.netlify.com/sites/vitest-dev/deploys/67077882ce54780008dca6dc
😎 Deploy Preview https://deploy-preview-6676--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@privatenumber privatenumber marked this pull request as ready for review October 10, 2024 04:11

```ts
// test-setup.ts
import type {} from 'vitest/globals'
Copy link
Author

@privatenumber privatenumber Oct 10, 2024

Choose a reason for hiding this comment

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

It's important to use an explicit type import here so it can be stripped by the compiler.

If there's no type annotation, it will be left to be resolved at run-time, and it can fail if there's no default condition:
https://github.com/vitest-dev/vitest/blob/main/packages/vitest/package.json#L37-L39

If there can be a default condition pointing to an empty file, we can simplify the recommendation to import 'vitest/globals' but I think it's better to be explicit.


```ts
import 'vitest/globals'
```
Copy link
Author

@privatenumber privatenumber Oct 10, 2024

Choose a reason for hiding this comment

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

In my experiments, this is the best method because it's clear, doesn't require any hack like import type {}, doesn't pollute source code, and works even if types/typeRoots are overridden in tsconfig.json.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should also mention it here in the docs?


Alternatively, add `vitest/globals` to the `types` field in `tsconfig.json`.

Warning: This disables TypeScript's automatic type detection, which could break your setup if your project relies on it. If you encounter types missing from other packages, you'll now need to manually register them here as well.
Copy link
Author

Choose a reason for hiding this comment

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

I think we should not even include this method as many beginners have been following this without understanding how disruptive it is and makes TS harder for them to work with.

Copy link
Member

Choose a reason for hiding this comment

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

Vitepress has a ::: danger block we can use here

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.

2 participants