Skip to content
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 @@ -226,6 +226,46 @@ export default createRule<Options, MessageIds>({
return type.isLiteral() || tsutils.isBooleanLiteralType(type);
}

function isIIFE(
expression: TSESTree.Expression,
): expression is TSESTree.CallExpression & {
callee: TSESTree.ArrowFunctionExpression | TSESTree.FunctionExpression;
} {
return (
expression.type === AST_NODE_TYPES.CallExpression &&
(expression.callee.type === AST_NODE_TYPES.ArrowFunctionExpression ||
expression.callee.type === AST_NODE_TYPES.FunctionExpression)
);
}

function getUncastType(
node: TSESTree.TSAsExpression | TSESTree.TSTypeAssertion,
): ts.Type {
// Special handling for IIFE: extract the function's return type
if (isIIFE(node.expression)) {
const callee = node.expression.callee;
const functionType = services.getTypeAtLocation(callee);
const signatures = functionType.getCallSignatures();

if (signatures.length > 0) {
const returnType = checker.getReturnTypeOfSignature(signatures[0]);

// If the function has no explicit return type annotation and returns undefined,
// treat it as void (TypeScript infers () => {} as () => undefined, but it should be void)
if (
callee.returnType == null &&
isTypeFlagSet(returnType, ts.TypeFlags.Undefined)
) {
return checker.getVoidType();
}

return returnType;
}
}

return services.getTypeAtLocation(node.expression);
}

return {
'TSAsExpression, TSTypeAssertion'(
node: TSESTree.TSAsExpression | TSESTree.TSTypeAssertion,
Expand All @@ -252,7 +292,8 @@ export default createRule<Options, MessageIds>({
return;
}

const uncastType = services.getTypeAtLocation(node.expression);
const uncastType = getUncastType(node);

const typeIsUnchanged = isTypeUnchanged(uncastType, castType);
const wouldSameTypeBeInferred = castTypeIsLiteral
? isImplicitlyNarrowedLiteralDeclaration(node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,60 @@ declare const a: T.Value1;
const b = a as const;
`,
},
{
code: `
(() => {})() as undefined;
`,
},
{
code: `
const f = () => {};
f() as undefined;
`,
},
{
code: `
(function () {})() as undefined;
`,
},
{
code: `
interface Overloaded {
(): undefined;
(value: string): void;
}

((value => {}) as Overloaded)('') as undefined;
`,
},
{
code: `
interface Overloaded {
(): void;
(value: string): undefined;
}

((() => {}) as Overloaded)() as undefined;
`,
},
{
code: `
interface GenericOverloaded {
<T extends string>(value: T): void;
(): undefined;
}
((value => {}) as GenericOverloaded)('') as undefined;
`,
},
{
code: `
interface Unioned {
(): undefined | void;
}

((() => {}) as Unioned)() as undefined;
`,
},
],

invalid: [
Expand Down Expand Up @@ -1442,5 +1496,54 @@ declare const a: T.Value1;
const b = a;
`,
},
{
code: `
((): undefined => {})() as undefined;
`,
errors: [
{
messageId: 'unnecessaryAssertion',
},
],
output: `
((): undefined => {})();
`,
},
{
code: `
(() => 1)() as number;
`,
errors: [
{
messageId: 'unnecessaryAssertion',
},
],
output: `
(() => 1)();
`,
},
{
code: `
interface Overloaded {
(): void;
(value: string): undefined;
}

((value => {}) as Overloaded)('') as undefined;
`,
errors: [
{
messageId: 'unnecessaryAssertion',
},
],
output: `
interface Overloaded {
(): void;
(value: string): undefined;
}

((value => {}) as Overloaded)('');
`,
},
],
});
Loading