JoshuaKGoldberg / JoshuaKGoldberg/TypeStat

🐛 Bug: strictNonNullAssertions changes "return undefined" to "return undefined!"

Open
#1,516 1 comment 0 reactions 0 assignees View on GitHub
status: accepting prs type: bug
Dominant language
TypeScript
Stars
2.3k
Forks
47
Avg merge
15h 55m
Merged PRs (30d)
21

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/TypeStat/issues?q=is%3Aissue) and found none that matched my issue.

### Expected

It should allow returning undefined here

```ts
const collectFileNamesFromGlobs = async (
argv: TypeStatArgv,
cwd: string,
include: readonly string[] | undefined,
): Promise<[readonly string[], readonly string[]] | undefined> => {
if (argv.args.length) {
return [argv.args, await glob(argv.args)];
}

if (include === undefined) {
return undefined;
}

return [
include,
await glob(include.map((subInclude) => path.join(cwd, subInclude))),
];
};
```

### Actual

The undefined return is changed to this

```ts
if (include === undefined) {
return undefined!;
}
```

### Additional Info

This is probably related to https://github.com/JoshuaKGoldberg/TypeStat/issues/1494 - it does not understand that the function accepts return type `Promise`.

typestat.json
```json
[
{
"fixes": {
"strictNonNullAssertions": true
},
"include": [
"src/**/*.{ts,tsx}"
],
"projectPath": "./tsconfig.json",
"types": {
"strictNullChecks": true
}
}
]
```

tsconfig
```tsconfig.json
{
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"esModuleInterop": true,
"module": "NodeNext",
"moduleResolution": "NodeNext",
"noEmit": true,
"outDir": "lib",
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"target": "ES2022"
},
"include": ["src", "test/*.ts"]
}
```

Contributor guide

Open the contributing guide

Research direction

Use the provided TypeScript function and typestat.json configuration as the reproduction, focusing on the strictNonNullAssertions fix. Trace how the return type Promise<[readonly string[], readonly string[]] | undefined> is handled when returning undefined, then add a regression test showing that the return remains undefined rather than becoming undefined!.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.