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-unnecessary-type-assertion] allow non-null assertion for void type #8912

Merged
merged 2 commits into from
Apr 22, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/eslint-plugin/src/rules/no-unnecessary-type-assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export default createRule<Options, MessageIds>({

const type = getConstrainedTypeAtLocation(services, node.expression);

if (!isNullableType(type)) {
if (!isNullableType(type) && !isTypeFlagSet(type, ts.TypeFlags.Void)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question - should void just be part of isNullableType?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kirkwaiblinger A quick check shows that modifying isNullableType seems to work fine. However, if we do modify this function, it would be nice to add some tests to other rules that use it. What do you think about creating a separate issue?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed!

if (
node.expression.type === AST_NODE_TYPES.Identifier &&
isPossiblyUsedBeforeAssigned(node.expression)
Expand Down Expand Up @@ -217,6 +217,7 @@ export default createRule<Options, MessageIds>({
ts.TypeFlags.Undefined,
);
const typeIncludesNull = isTypeFlagSet(type, ts.TypeFlags.Null);
const typeIncludesVoid = isTypeFlagSet(type, ts.TypeFlags.Void);

const contextualTypeIncludesUndefined = isTypeFlagSet(
contextualType,
Expand All @@ -226,6 +227,10 @@ export default createRule<Options, MessageIds>({
contextualType,
ts.TypeFlags.Null,
);
const contextualTypeIncludesVoid = isTypeFlagSet(
contextualType,
ts.TypeFlags.Void,
);

// make sure that the parent accepts the same types
// i.e. assigning `string | null | undefined` to `string | undefined` is invalid
Expand All @@ -235,8 +240,11 @@ export default createRule<Options, MessageIds>({
const isValidNull = typeIncludesNull
? contextualTypeIncludesNull
: true;
const isValidVoid = typeIncludesVoid
? contextualTypeIncludesVoid
: true;

if (isValidUndefined && isValidNull) {
if (isValidUndefined && isValidNull && isValidVoid) {
context.report({
node,
messageId: 'contextuallyUnnecessary',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ let foo: number = bar!;
declare const a: { data?: unknown };
const x = a.data!;
`,
`
declare function foo(arg?: number): number | void;
const bar: number = foo()!;
`,
{
code: `
Expand Down Expand Up @@ -692,6 +696,24 @@ y = 0;
},
{
code: `
declare function foo(arg?: number): number | void;
const bar: number | void = foo()!;
`,
output: `
declare function foo(arg?: number): number | void;
const bar: number | void = foo();
`,
errors: [
{
messageId: 'contextuallyUnnecessary',
line: 3,
column: 28,
endColumn: 34,
},
],
},
{
code: `
declare function foo(): number;
const a = foo()!;
`,
Expand Down