Skip to content
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

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

Merged
merged 4 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 4 additions & 7 deletions 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 All @@ -589,8 +587,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
35 changes: 20 additions & 15 deletions packages/type-utils/src/predicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,36 @@ 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');

/**
* Checks if the given type is (or accepts) nullable
* @param isReceiver true if the type is a receiving type (i.e. the type of a called function's parameter)
*/
export function isNullableType(
type: ts.Type,
{
isReceiver = false,
allowUndefined = true,
}: { isReceiver?: boolean; allowUndefined?: boolean } = {},
isReceiver,
allowUndefined,
}?: {
/**
* @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;
},
): 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