Skip to content

linter: unicorn/prefer-native-coercion-functions doesn't consider type guard #23773

Description

@elusiveunit

What version of Oxlint are you using?

1.71.0

What command did you run?

npx oxlint

What does your .oxlintrc.json (or oxlint.config.ts) config file look like?

export default defineConfig({
	plugins: ['unicorn'],
	rules: {
		'unicorn/prefer-native-coercion-functions': 'warn',
	},
});

What happened?

test.ts

const yes = 'str';
const maybe = undefined as string | undefined;
const things = [yes, maybe].filter((thing): thing is string => Boolean(thing));
function doStuff(stuff: string[]) {
	return stuff;
}
doStuff(things);

Output from npx oxlint:

  ⚠ unicorn(prefer-native-coercion-functions): The function is equivalent to `Boolean`. Call `Boolean` directly.
   ╭─[test.ts:3:36]
 2 │ const maybe = undefined as string | undefined;
 3 │ const things = [yes, maybe].filter((thing): thing is string => Boolean(thing));
   ·                                    ──────────────────────────────────────────
 4 │ function doStuff(stuff: string[]) {
   ╰────

Upstream has the same issue. Had, was fixed in v64.0.0. Doing the suggested fix to const things = [yes, maybe].filter(Boolean); leads to a type error since Boolean doesn't narrow:

test.ts:7:9 - error TS2345: Argument of type '(string | undefined)[]' is not assignable to parameter of type 'string[]'.
  Type 'string | undefined' is not assignable to type 'string'.
    Type 'undefined' is not assignable to type 'string'.

doStuff(things);
        ~~~~~~

A call without a return type (const things = [yes, maybe].filter((thing) => Boolean(thing))) is a valid lint error since things is still (string | undefined)[].

Metadata

Metadata

Assignees

Labels

Type

Fields

Priority

None yet

Effort

None yet

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions