Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy 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 fromall 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
---
description: 'Enforce non-null assertions over explicit typecasts.'
description: 'Enforce non-null assertions over explicit typeassertions.'
---

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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
---
description: 'Enforce using type parameter when calling `Array#reduce` instead ofcasting.'
description: 'Enforce using type parameter when calling `Array#reduce` instead ofusing 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 withcasting.
This means that TypeScript doesn't have to try to infer the type, and avoids the common pitfalls that come withassertions.

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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -84,7 +84,7 @@ function foo(bool?: boolean) {
}
}

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

Expand DownExpand 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 !== ""`)
- Explicitlycast value to a boolean (`str` → `Boolean(str)`)
- Explicitlyconvert 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)`)
- Explicitlycast value to a boolean (`num` → `Boolean(num)`)
- Explicitlyconvert 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 ?? ""`)
- Explicitlycast value to a boolean (`maybeStr` → `Boolean(maybeStr)`)
- Explicitlyconvert 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`)
- Explicitlycast value to a boolean (`maybeNum` → `Boolean(maybeNum)`)
- Explicitlyconvert value to a boolean (`maybeNum` → `Boolean(maybeNum)`)
- `any` and `unknown` - Provides following suggestions:
- Explicitlycast value to a boolean (`value` → `Boolean(value)`)
- Explicitlyconvert value to a boolean (`value` → `Boolean(value)`)

## When Not To Use It

Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,9 +22,9 @@ export default createRule({
},
messages: {
unsafeOfAnyTypeAssertion:
'Unsafecast from {{type}} detected: consider using type guards or a safercast.',
'Unsafeassertion from {{type}} detected: consider using type guards or a saferassertion.',
unsafeToAnyTypeAssertion:
'Unsafecast to {{type}} detected: consider using a more specific type to ensure safety.',
'Unsafeassertion 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 DownExpand Up@@ -62,7 +62,7 @@ export default createRule({
return;
}

// handle cases whencasting unknown ==> any.
// handle cases whenasserting unknown ==> any.
if (isTypeAnyType(assertedType) && isTypeUnknownType(expressionType)) {
context.report({
node,
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,7 +16,7 @@ export default createRule({
meta: {
type: 'suggestion',
docs: {
description: 'Enforce non-null assertions over explicit typecasts',
description: 'Enforce non-null assertions over explicit typeassertions',
recommended: 'stylistic',
requiresTypeChecking: true,
},
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,14 +21,14 @@ export default createRule({
type: 'problem',
docs: {
description:
'Enforce using type parameter when calling `Array#reduce` instead ofcasting',
'Enforce using type parameter when calling `Array#reduce` instead ofusing a type assertion',
recommended: 'strict',
requiresTypeChecking: true,
},
fixable: 'code',
messages: {
preferTypeParameter:
'Unnecessarycast: Array#reduce accepts a type parameter for the default value.',
'Unnecessaryassertion: Array#reduce accepts a type parameter for the default value.',
},
schema: [],
},
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -68,7 +68,7 @@ export default createRule<Options, MessageId>({
messages: {
conditionErrorAny:
'Unexpected any value in conditional. ' +
'An explicit comparison or typecast is required.',
'An explicit comparison or typeconversion is required.',
conditionErrorNullableBoolean:
'Unexpected nullable boolean value in conditional. ' +
'Please handle the nullish case explicitly.',
Expand DownExpand Up@@ -100,7 +100,7 @@ export default createRule<Options, MessageId>({
'Unexpected string value in conditional. ' +
'An explicit empty string check is required.',
conditionFixCastBoolean:
'Explicitlycast value to a boolean (`Boolean(value)`)',
'Explicitlyconvert 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 abouthow customized files appear on GitHub.

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

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

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

Loading

[8]ページ先頭

©2009-2025 Movatter.jp