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

Appearance settings

fix(eslint-plugin): [no-unnecesary-type-assertion] treat unknown/any as nullable #8089

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 4 commits into from
Jan 12, 2024
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
11 changes: 4 additions & 7 deletions 11 packages/eslint-plugin/src/rules/no-unnecessary-condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ export default createRule<Options, MessageId>({
propertyType.value.toString(),
);
if (propType) {
return isNullableType(propType, { allowUndefined: true });
return isNullableType(propType);
}
}
const typeName = getTypeName(checker, propertyType);
Expand Down Expand Up @@ -568,14 +568,12 @@ export default createRule<Options, MessageId>({
);

if (propType) {
return isNullableType(propType, { allowUndefined: true });
return isNullableType(propType);
}

return !!checker.getIndexInfoOfType(type, ts.IndexKind.String);
});
return (
!isOwnNullable && isNullableType(prevType, { allowUndefined: true })
);
return !isOwnNullable && isNullableType(prevType);
}
return false;
}
Expand Down Expand Up @@ -612,8 +610,7 @@ export default createRule<Options, MessageId>({
const possiblyVoid = isTypeFlagSet(type, ts.TypeFlags.Void);
return (
isTypeFlagSet(type, ts.TypeFlags.Any | ts.TypeFlags.Unknown) ||
(isOwnNullable &&
(isNullableType(type, { allowUndefined: true }) || possiblyVoid))
(isOwnNullable && (isNullableType(type) || possiblyVoid))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,7 @@ export default createRule<Options, MessageIds>({
): void {
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);
const type = checker.getTypeAtLocation(tsNode.left);
const isNullish = isNullableType(type, { allowUndefined: true });
if (!isNullish) {
if (!isNullableType(type)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ const foo = { 0: 'hello', 5: 'hello' } as PossibleTuple;
`
let bar: number | undefined = x;
let foo: number = bar!;
`,
`
declare const a: { data?: unknown };

const x = a.data!;
`,
{
code: `
Expand Down
26 changes: 13 additions & 13 deletions 26 packages/type-utils/src/predicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ import debug from 'debug';
import * as tsutils from 'ts-api-utils';
import * as ts from 'typescript';

import { getTypeFlags, isTypeFlagSet } from './typeFlagUtils';
import { isTypeFlagSet } from './typeFlagUtils';

const log = debug('typescript-eslint:eslint-plugin:utils:types');

export interface IsNullableTypeOptions {
/**
* Whether the type is a receiving type (i.e. the type of a called function's parameter).
* @deprecated - this flag no longer does anything and will be removed in the next major
*/
isReceiver?: boolean;
/**
* @deprecated - this flag no longer does anything and will be removed in the next major
*/
allowUndefined?: boolean;
}

Expand All @@ -19,18 +22,15 @@ export interface IsNullableTypeOptions {
*/
export function isNullableType(
type: ts.Type,
{ isReceiver = false, allowUndefined = true }: IsNullableTypeOptions = {},
_deprecated?: IsNullableTypeOptions,
): boolean {
const flags = getTypeFlags(type);

if (isReceiver && flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) {
return true;
}

if (allowUndefined) {
return (flags & (ts.TypeFlags.Null | ts.TypeFlags.Undefined)) !== 0;
}
return (flags & ts.TypeFlags.Null) !== 0;
return isTypeFlagSet(
type,
ts.TypeFlags.Any |
ts.TypeFlags.Unknown |
ts.TypeFlags.Null |
ts.TypeFlags.Undefined,
);
}

/**
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.