Drop the contextual report of no-unnecessary-type-assertion on as

typescript-eslint 8 reports `x as T` as unnecessary when the receiver
would accept `x` unchanged, and its autofix deletes the assertion. That
is unsound when the assertion also drives inference around it:

  const { validators } = found || { validators: {} as ValPoints };

loses the type of `validators`, and every later use becomes an
unresolved type (api-derive's stakerRewards after an autofix). It also
deletes assertions kept on purpose for another environment's typings.
Across api, common, ui and apps it raised 45 reports.

The plugin is now registered through withoutContextualAssertions, which
drops only that report on `as` / `<T>` assertions. Assertions to the
type an expression already has are still reported and fixed, and so
are non-null assertions a receiver makes unnecessary. A spec lints a
fixture through the real type checker with the plugin wrapped and as
published, so it fails if the filter stops filtering.
This commit is contained in:
Satoshi Qazi Muhammed authored and SatoshiQaziMuhammed committed 2026-10-09 15:36:02 +04:00
1 parent cd810d5ea2
commit 05b2fdafad
3 files changed
+154 -1

No files matched your search

+3 -1
View File
@@ -24,6 +24,7 @@ import globals from 'globals';
import { overrideAll, overrideJs, overrideJsx, overrideSpec } from './eslint.rules.js';
import { standardRules } from './eslint.standard.js';
import { withoutContextualAssertions } from './eslint.typeAssertion.js';
/**
* eslint-plugin-header declares no options schema, and eslint 9+ rejects
@@ -93,7 +94,8 @@ export default [
},
plugins: {
'@stylistic': stylisticPlugin,
'@typescript-eslint': tsPlugin,
// see eslint.typeAssertion.js
'@typescript-eslint': withoutContextualAssertions(tsPlugin),
// eslint-plugin-header reports at the first token after the leading
// comments, so a generated file's /* eslint-disable */ covers it; the
// maintained fork reports at line 1 instead. The compat layer supplies the
@@ -0,0 +1,65 @@
// Copyright 2017-2026 @pezkuwi/dev authors & contributors
// SPDX-License-Identifier: Apache-2.0
const RULE = 'no-unnecessary-type-assertion';
const ASSERTIONS = ['TSAsExpression', 'TSTypeAssertion'];
/**
* typescript-eslint 8 added a second check to no-unnecessary-type-assertion:
* an `x as T` is reported as unnecessary when the place it is passed to would
* accept `x` as it is ("the receiver accepts the original type"), and the
* autofix deletes the assertion. That is unsound when the assertion also
* drives inference around it: in
*
* const { validators } = found || { validators: {} as ValPoints };
*
* deleting `as ValPoints` leaves `validators` typed `{}`, and every use of it
* after that is an unresolved type. It also deletes assertions kept on
* purpose for another environment's typings (a Ledger transport under
* esm.sh). Measured across the consumers before this was added: 45 such
* reports, the first of them breaking the types of api-derive.
*
* This keeps the rule as it was before that check: an assertion to the type
* the expression already has is still reported and fixed, and so are non-null
* assertions the receiver makes unnecessary. Only the contextual report on
* `as` / `<T>` assertions is dropped.
*
* @template {object} P
* @param {P} plugin the @typescript-eslint plugin
* @returns {P}
*/
export function withoutContextualAssertions (plugin) {
const rules = /** @type {Record<string, import('eslint').Rule.RuleModule>} */ (/** @type {{ rules: unknown }} */ (plugin).rules);
const rule = rules[RULE];
return /** @type {P} */ ({
...plugin,
rules: {
...rules,
[RULE]: {
...rule,
/** @param {import('eslint').Rule.RuleContext} context */
create (context) {
const filtered = Object.create(context, {
report: {
value: (/** @type {import('eslint').Rule.ReportDescriptor} */ descriptor) => {
if (
'messageId' in descriptor &&
descriptor.messageId === 'contextuallyUnnecessary' &&
'node' in descriptor &&
ASSERTIONS.includes(descriptor.node.type)
) {
return;
}
context.report(descriptor);
}
}
});
return rule.create(filtered);
}
}
}
});
}