Skip to content

fix(eslint-plugin): use consistent naming for asserting types and casting values #10472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ let funcExpr: FuncType = function () {
};

let asTyped = (() => '') as () => string;
let castTyped = <() => string>(() => '');

interface ObjectType {
foo(): number;
Expand All @@ -172,9 +171,6 @@ let objectProp: ObjectType = {
let objectPropAs = {
foo: () => 1,
} as ObjectType;
let objectPropCast = <ObjectType>{
foo: () => 1,
};

declare function functionWithArg(arg: () => number);
functionWithArg(() => 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ export let funcExpr: FuncType = function () {
};

export let asTyped = (() => '') as () => string;
export let castTyped = <() => string>(() => '');

interface ObjectType {
foo(): number;
Expand All @@ -253,9 +252,6 @@ export let objectProp: ObjectType = {
export let objectPropAs = {
foo: () => 1,
} as ObjectType;
export let objectPropCast = <ObjectType>{
foo: () => 1,
};

type FooType = (bar: string) => void;
export const foo: FooType = bar => {};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
description: 'Enforce non-null assertions over explicit type casts.'
description: 'Enforce non-null assertions over explicit type assertions.'
---

import Tabs from '@theme/Tabs';
Expand All @@ -15,7 +15,7 @@ There are two common ways to assert to TypeScript that a value is its type witho
- `as`: Traditional type assertion with a coincidentally equivalent type

`!` non-null assertions are generally preferred for requiring less code and being harder to fall out of sync as types change.
This rule reports when an `as` cast is doing the same job as a `!` would, and suggests fixing the code to be an `!`.
This rule reports when an `as` assertion is doing the same job as a `!` would, and suggests fixing the code to be an `!`.

## Examples

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
description: 'Enforce using type parameter when calling `Array#reduce` instead of casting.'
description: 'Enforce using type parameter when calling `Array#reduce` instead of using a type assertion.'
---

import Tabs from '@theme/Tabs';
Expand All @@ -19,7 +19,7 @@ A common solution to this problem is to use an `as` assertion on the initial val
While this will work, it's not the most optimal solution as type assertions have subtle effects on the underlying types that can allow bugs to slip in.

A better solution is to pass the type in as a generic type argument to `Array#reduce` explicitly.
This means that TypeScript doesn't have to try to infer the type, and avoids the common pitfalls that come with casting.
This means that TypeScript doesn't have to try to infer the type, and avoids the common pitfalls that come with assertions.

This rule looks for calls to `Array#reduce`, and reports if an initial value is being passed & asserted.
It will suggest instead pass the asserted type to `Array#reduce` as a generic type argument.
Expand Down
12 changes: 6 additions & 6 deletions packages/eslint-plugin/docs/rules/strict-boolean-expressions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function foo(bool?: boolean) {
}
}

// `any` types should be cast to boolean explicitly
// `any` types should be converted to boolean explicitly
const foo = (arg: any) => (Boolean(arg) ? 1 : 0);
```

Expand Down Expand Up @@ -176,14 +176,14 @@ This rule provides following fixes and suggestions for particular types in boole
- `string` - (when `allowString` is `false`) - Provides following suggestions:
- Change condition to check string's length (`str` → `str.length > 0`)
- Change condition to check for empty string (`str` → `str !== ""`)
- Explicitly cast value to a boolean (`str` → `Boolean(str)`)
- Explicitly convert value to a boolean (`str` → `Boolean(str)`)
- `number` - (when `allowNumber` is `false`):
- For `array.length` - Provides **autofix**:
- Change condition to check for 0 (`array.length` → `array.length > 0`)
- For other number values - Provides following suggestions:
- Change condition to check for 0 (`num` → `num !== 0`)
- Change condition to check for NaN (`num` → `!Number.isNaN(num)`)
- Explicitly cast value to a boolean (`num` → `Boolean(num)`)
- Explicitly convert value to a boolean (`num` → `Boolean(num)`)
- `object | null | undefined` - (when `allowNullableObject` is `false`) - Provides **autofix**:
- Change condition to check for null/undefined (`maybeObj` → `maybeObj != null`)
- `boolean | null | undefined` - Provides following suggestions:
Expand All @@ -192,13 +192,13 @@ This rule provides following fixes and suggestions for particular types in boole
- `string | null | undefined` - Provides following suggestions:
- Change condition to check for null/undefined (`maybeStr` → `maybeStr != null`)
- Explicitly treat nullish value the same as an empty string (`maybeStr` → `maybeStr ?? ""`)
- Explicitly cast value to a boolean (`maybeStr` → `Boolean(maybeStr)`)
- Explicitly convert value to a boolean (`maybeStr` → `Boolean(maybeStr)`)
- `number | null | undefined` - Provides following suggestions:
- Change condition to check for null/undefined (`maybeNum` → `maybeNum != null`)
- Explicitly treat nullish value the same as 0 (`maybeNum` → `maybeNum ?? 0`)
- Explicitly cast value to a boolean (`maybeNum` → `Boolean(maybeNum)`)
- Explicitly convert value to a boolean (`maybeNum` → `Boolean(maybeNum)`)
- `any` and `unknown` - Provides following suggestions:
- Explicitly cast value to a boolean (`value` → `Boolean(value)`)
- Explicitly convert value to a boolean (`value` → `Boolean(value)`)

## When Not To Use It

Expand Down
6 changes: 3 additions & 3 deletions packages/eslint-plugin/src/rules/no-unsafe-type-assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export default createRule({
},
messages: {
unsafeOfAnyTypeAssertion:
'Unsafe cast from {{type}} detected: consider using type guards or a safer cast.',
'Unsafe assertion from {{type}} detected: consider using type guards or a safer assertion.',
unsafeToAnyTypeAssertion:
'Unsafe cast to {{type}} detected: consider using a more specific type to ensure safety.',
'Unsafe assertion to {{type}} detected: consider using a more specific type to ensure safety.',
unsafeTypeAssertion:
"Unsafe type assertion: type '{{type}}' is more narrow than the original type.",
},
Expand Down Expand Up @@ -62,7 +62,7 @@ export default createRule({
return;
}

// handle cases when casting unknown ==> any.
// handle cases when asserting unknown ==> any.
if (isTypeAnyType(assertedType) && isTypeUnknownType(expressionType)) {
context.report({
node,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default createRule({
meta: {
type: 'suggestion',
docs: {
description: 'Enforce non-null assertions over explicit type casts',
description: 'Enforce non-null assertions over explicit type assertions',
recommended: 'stylistic',
requiresTypeChecking: true,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ export default createRule({
type: 'problem',
docs: {
description:
'Enforce using type parameter when calling `Array#reduce` instead of casting',
'Enforce using type parameter when calling `Array#reduce` instead of using a type assertion',
recommended: 'strict',
requiresTypeChecking: true,
},
fixable: 'code',
messages: {
preferTypeParameter:
'Unnecessary cast: Array#reduce accepts a type parameter for the default value.',
'Unnecessary assertion: Array#reduce accepts a type parameter for the default value.',
},
schema: [],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default createRule<Options, MessageId>({
messages: {
conditionErrorAny:
'Unexpected any value in conditional. ' +
'An explicit comparison or type cast is required.',
'An explicit comparison or type conversion is required.',
conditionErrorNullableBoolean:
'Unexpected nullable boolean value in conditional. ' +
'Please handle the nullish case explicitly.',
Expand Down Expand Up @@ -100,7 +100,7 @@ export default createRule<Options, MessageId>({
'Unexpected string value in conditional. ' +
'An explicit empty string check is required.',
conditionFixCastBoolean:
'Explicitly cast value to a boolean (`Boolean(value)`)',
'Explicitly convert value to a boolean (`Boolean(value)`)',

conditionFixCompareEmptyString:
'Change condition to check for empty string (`value !== ""`)',
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading