TS2344 in `result-all.d.ts` with `exactOptionalPropertyTypes` since 10.2.0
Since v10.2.0, TypeScript reports an error inside Execa's own declarations when a project enables exactOptionalPropertyTypes and does not use skipLibCheck. v10.1.1 type-checks cleanly with the same setup.
Reproduction
package.json:
{
"type": "module",
"dependencies": { "execa": "10.2.0" },
"devDependencies": { "@types/node": "26.6.5", "typescript": "7.0.2" }
}
tsconfig.json:
{
"compilerOptions": {
"strict": true,
"exactOptionalPropertyTypes": true,
"module": "nodenext",
"target": "es2022",
"noEmit": true,
"skipLibCheck": false,
"types": ["node"]
}
}
index.ts:
import {execa} from 'execa';
await execa('echo', ['hi']);
npx tsc -p .:
node_modules/execa/types/return/result-all.d.ts(29,21): error TS2344: Type 'Omit<OptionsType, "buffer"> & { buffer: { stdout: BufferStdout; stderr: BufferStderr; }; }' does not satisfy the constraint 'CommonOptions'.
Types of property 'preferLocal' are incompatible.
Type 'OptionsType["preferLocal"]' is not assignable to type 'boolean'.
Type 'boolean | undefined' is not assignable to type 'boolean'.
Type 'undefined' is not assignable to type 'boolean'.
| execa | exactOptionalPropertyTypes: true |
exactOptionalPropertyTypes: false |
|---|---|---|
| 10.1.0 | passes | passes |
| 10.1.1 | passes | passes |
| 10.2.0 | TS2344 | passes |
TypeScript 6.0.3 reports the same error.
Cause
ResultAllBuffered in types/return/result-all.d.ts (added in f5772f3) passes Omit<OptionsType, 'buffer'> & {buffer: …} to ResultAllOutput<OptionsType extends CommonOptions>. For a generic OptionsType, Omit yields OptionsType['preferLocal'], which is boolean | undefined. Under exactOptionalPropertyTypes, that does not satisfy the optional readonly preferLocal?: boolean in CommonOptions.
I have a fix for this on a branch, since PRs here are limited to collaborators: MFA-G/execa@fix-exact-optional-result-all.
The Omit<OptionsType, 'buffer'> & {buffer: {stdout: BufferStdout; stderr: BufferStderr}} built in ResultAllBuffered fails CommonOptions under exactOptionalPropertyTypes for two reasons, and both need fixing:
Omitof a genericOptionsTypeis not accepted asCommonOptions(preferLocal: boolean | undefinedvs.preferLocal?: boolean). Wrapping it in anOptionsType extends CommonOptions ? ... : neverconditional, likeScriptOptionsintypes/methods/script.d.ts, fixes that.- The distributed flags can be
undefined, and{stdout: undefined}is not a validbuffervalue under the flag. The branch leaves the key out instead.FdSpecificOptionalready reads a missing key asundefined.
The emitted result.all types for a set of option combinations (all alone, buffer: false, buffer: {stdout: false}, buffer: {stderr: false} with encoding: 'buffer', lines, stdout: 'ignore' with fd-specific lines, execaSync with preferLocal, an optional buffer?: boolean) are unchanged without the flag, and the same with it. The branch also makes npm run type run tsc --exactOptionalPropertyTypes on the declarations, which fails on main with the error above and passes with the fix. I didn't enable the flag in tsconfig.json, because the test-d files aren't written for it.
Feel free to take the change as is, or let me know if you'd like it opened as a PR.