JoshuaKGoldberg / JoshuaKGoldberg/create-typescript-app

📝 Docs: Explain pnpm and declaration emit awkwardness

Open
#1,244 3 comments 0 reactions 0 assignees View on GitHub
area: documentation status: accepting prs type: feature
Dominant language
TypeScript
Stars
1.4k
Forks
89
Avg merge
1m
Merged PRs (30d)
3

Description

### Bug Report Checklist

- [X] I have tried restarting my IDE and the issue persists.
- [X] I have pulled the latest `main` branch of the repository.
- [X] I have [searched for related issues](https://github.com/JoshuaKGoldberg/create-typescript-app/issues?q=is%3Aissue) and found none that matched my issue.

### Expected

I expected to be able to define a helper module that imported `vitest` without error:

```ts
import { vi } from "vitest";

export const mockError = () =>
vi.spyOn(console, "error").mockImplementation(() => undefined);
```

### Actual

I got an extremely cryptic error:

```ts
import { vi } from "vitest";

export const mockError = () =>
// ~~~~~~~~~
// The inferred type of 'mockError' cannot be named without a reference to
// '.pnpm/@vitest+spy@1.1.3/node_modules/@vitest/spy'.
// This is likely not portable. A type annotation is necessary. ts(2742)
vi.spyOn(console, "error").mockImplementation(() => undefined);
```

### Additional Info

Here's an example of this error happening:
- failing build: https://github.com/danvk/gravlax/actions/runs/7545323899/job/20540667198
- failing commit: https://github.com/danvk/gravlax/pull/16/commits/86b060cc576c5ce297ba6438cb87c5225d08dc7b

The gist seems to be that `declaration: true` and `pnpm` don't work very well together. There are [many, many issues][1] about this on the TypeScript issue tracker. The original is https://github.com/microsoft/TypeScript/issues/29808.

This seems to be the canonical comment discussing workarounds: https://github.com/microsoft/TypeScript/issues/47663#issuecomment-1519138189

In my case I just turned off `.d.ts` emit, though this isn't ideal.

Dropping pnpm seems a little drastic, but this isn't great and `create-typescript-app` pushes you towards this error with its defaults. At the very least there could be something about this in the FAQ. A link to that comment in particular would have saved me some time today.

[1]: https://github.com/microsoft/TypeScript/issues?q=is%3Aissue+is%3Aopen+cannot+be+named+without+a+reference+portable+pnpm

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.