-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
base: main
Are you sure you want to change the base?
Conversation
✅ Deploy Preview for vitest-dev ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify site configuration. |
|
||
```ts | ||
// test-setup.ts | ||
import type {} from 'vitest/globals' |
There was a problem hiding this comment.
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.
70f17f8
to
8e479cc
Compare
8e479cc
to
67dce54
Compare
|
||
```ts | ||
import 'vitest/globals' | ||
``` |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
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.
pnpm-lock.yaml
unless you introduce a new test example.Tests
pnpm test:ci
.Documentation
pnpm run docs
command.Changesets
feat:
,fix:
,perf:
,docs:
, orchore:
.Problem
Currently, users are encouraged to use the
types
property intsconfig.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