Repository navigation
Description
Environment information
Details
CLI:
Version: 2.5.7
Color support: false
Platform:
CPU Architecture: x86_64
OS: windows
Environment:
BIOME_DISTRIBUTION: npm
BIOME_LOG_PATH: unset
BIOME_LOG_PREFIX_NAME: unset
BIOME_LOG_LEVEL: unset
BIOME_LOG_KIND: unset
BIOME_CONFIG_PATH: unset
BIOME_THREADS: unset
BIOME_WATCHER_KIND: unset
BIOME_WATCHER_POLLING_INTERVAL: unset
NO_COLOR: 1
TERM: xterm-256color
JS_RUNTIME_VERSION: v24.16.0
JS_RUNTIME_NAME: node
NODE_PACKAGE_MANAGER: unset
Biome Configuration:
Status: Loaded successfully.
Path: biome.json
Formatter enabled: true
Linter enabled: true
Assist enabled: true
VCS enabled: false
HTML full support enabled: unset
Linter:
JavaScript enabled: unset
JSON enabled: unset
CSS enabled: unset
GraphQL enabled: unset
Recommended: unset
Enabled rules:
suspicious/noUnnecessaryConditions
Workspace:
Open Documents: 0Rule name
suspicious/noUnnecessaryConditions
Playground link
Expected result
No diagnostic. RegExp.prototype.exec returns RegExpExecArray | null, so the ?. is required however the RegExp was constructed.
Reproduction
// `RegExp.prototype.exec` returns `RegExpExecArray | null`, so `?.` is required.
export function capture(input: string): string | undefined {
return /a(b)/.exec(input)?.[1];
}
export function captureDynamic(input: string, tag: string): string {
return new RegExp(`<${tag}>([^<]*)`).exec(input)?.[1] ?? '';
}b-regexp-exec.ts:7:10 lint/suspicious/noUnnecessaryConditions โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
ร Unnecessary optional chaining.
> 7 โ return new RegExp(`<${tag}>([^<]*)`).exec(input)?.[1] ?? '';
โ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
i Replace ?. with ..
i The receiver is guaranteed to be non-nullish.
Found 1 error.
The suggested fix is unsafe: taking it turns a non-match from undefined into a TypeError at runtime.
Analysis
The two functions differ only in how the pattern is built, and only the second is flagged:
/a(b)/.exec(input)?.[1]โ not flagged, correct.new RegExp(...).exec(input)?.[1]โ flagged.
So the regex literal carries a correctly-nullable exec while the RegExp constructor call does not โ it looks like the constructed-instance path loses the | null from the return type rather than the rule mishandling the chain.
Possibly a facet of #5977 (globals resolved from hardcoded definitions rather than real lib.d.ts), but the literal/constructor asymmetry suggests it may be fixable ahead of that work.
Context
Found while trialling the type-aware rules on a TypeScript-first OOXML/xlsx library. Dynamically built tag patterns are common in its test assertions, so this is 10 of the 148 noUnnecessaryConditions diagnostics on that tree.
Code of Conduct
- I agree to follow Biome's Code of Conduct
Activity
github-actions commentedon Aug 8, 2026
โ Organic activity
No automation signals detected in the analyzed events.
View full analysis โ
This is an automated analysis by AgentScan
AlbinoGeek commentedon Sep 19, 2026
This also reproduces on
@biomejs/biome2.5.14 (current npm release), and it gets worse:following the rule's own suggested fix breaks the typechecker, and the type-correct
alternative to the fix gets a second, different false positive from the same rule.
Repro (
matchhere is aconstholding a regex literal, notnew RegExp(...)):Applying the suggested fix (
?.[1]->[1]) tobearerToken:biome lintnow reports 0 errors on this function, buttscfails:So on this rule as it stands, none of the three forms satisfies both tools:
match?.[1], the type-correct form, flagged as an unnecessary condition.match[1], Biome's own suggested fix for (1), passes Biome, failstscwith TS18047.if (!match) return null;, the type-correct guard alternative to (1), flagged asalways falsy.
Versions:
@biomejs/biome2.5.14,typescript7.0.2, Node 24.18.0, Linux x86_64.Full repro, both files and commands:
https://github.com/AlbinoGeek/biome-regexp-exec-nullable-noUnnecessaryConditions
docs(issue): record that the comment was filed on biomejs/biome#11278
๐ง biome-config-polished: noUnnecessaryConditions off until biomejs/biโฆ
chore(deps): update vitest, coverage-v8 and jsdom; hold Biome at 2.5.13
fix lint false positives from biome 2.5.14 RegExp.exec() type inference
RENOVATE: Update all non-major dependencies (#695)
Upgrade supported dependencies and adopt Effect rc117 (#358)
chore(deps): update jsdom to 30.1.1; keep Biome held at 2.5.13
Update dependency @biomejs/biome to v2.5.15 (#51)
Update dependency @biomejs/biome to v2.5.15 (#25)
Update dependency @biomejs/biome to v2.5.15 (#1775)
chore(lint): Biome 2.5.15 for the framework and new apps, noUnnecessaโฆ