Skip to content

Fix isolated declarations to not flag function expressions in call arguments #62125

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 6 additions & 1 deletion src/compiler/expressionToTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,9 @@ export function createSyntacticTypeNodeBuilder(
case SyntaxKind.ConstructorType:
case SyntaxKind.FunctionExpression:
case SyntaxKind.ArrowFunction:
// Don't report inference fallback for function expressions that are arguments to call expressions
const shouldReportFallback = !(node.kind === SyntaxKind.FunctionExpression || node.kind === SyntaxKind.ArrowFunction) || !isCallExpression(node.parent);
return createReturnFromSignature(node, symbol, context, shouldReportFallback);
case SyntaxKind.JSDocFunctionType:
case SyntaxKind.JSDocSignature:
return createReturnFromSignature(node, symbol, context);
Expand Down Expand Up @@ -968,7 +971,9 @@ export function createSyntacticTypeNodeBuilder(
return failed;
}
function typeFromFunctionLikeExpression(fnNode: FunctionExpression | ArrowFunction, context: SyntacticTypeNodeBuilderContext) {
const returnType = createReturnFromSignature(fnNode, /*symbol*/ undefined, context);
// Don't report inference fallback for function expressions that are arguments to call expressions
const shouldReportFallback = !isCallExpression(fnNode.parent);
const returnType = createReturnFromSignature(fnNode, /*symbol*/ undefined, context, shouldReportFallback);
const typeParameters = reuseTypeParameters(fnNode.typeParameters, context);
const parameters = fnNode.parameters.map(p => ensureParameter(p, context));
return syntacticResult(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
isolatedDeclarationsFunctionExpressionInCall.ts(4,14): error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations.
isolatedDeclarationsFunctionExpressionInCall.ts(8,14): error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations.
isolatedDeclarationsFunctionExpressionInCall.ts(12,14): error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations.
isolatedDeclarationsFunctionExpressionInCall.ts(16,14): error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations.


==== isolatedDeclarationsFunctionExpressionInCall.ts (4 errors) ====
declare function observer<T>(fn: T): T;
declare function action<T>(fn: T): T;

export const Component = observer(() => {
~~~~~~~~~
!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations.
!!! related TS9027 isolatedDeclarationsFunctionExpressionInCall.ts:4:14: Add a type annotation to the variable Component.
return "hello";
});

export const thing = action(function () {
~~~~~
!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations.
!!! related TS9027 isolatedDeclarationsFunctionExpressionInCall.ts:8:14: Add a type annotation to the variable thing.
return Component;
});

export const arrowWithType = observer((): string => {
~~~~~~~~~~~~~
!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations.
!!! related TS9027 isolatedDeclarationsFunctionExpressionInCall.ts:12:14: Add a type annotation to the variable arrowWithType.
return "typed";
});

export const functionWithType = action(function (): typeof Component {
~~~~~~~~~~~~~~~~
!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations.
!!! related TS9027 isolatedDeclarationsFunctionExpressionInCall.ts:16:14: Add a type annotation to the variable functionWithType.
return Component;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//// [tests/cases/compiler/isolatedDeclarationsFunctionExpressionInCall.ts] ////

//// [isolatedDeclarationsFunctionExpressionInCall.ts]
declare function observer<T>(fn: T): T;
declare function action<T>(fn: T): T;

export const Component = observer(() => {
return "hello";
});

export const thing = action(function () {
return Component;
});

export const arrowWithType = observer((): string => {
return "typed";
});

export const functionWithType = action(function (): typeof Component {
return Component;
});

//// [isolatedDeclarationsFunctionExpressionInCall.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.functionWithType = exports.arrowWithType = exports.thing = exports.Component = void 0;
exports.Component = observer(function () {
return "hello";
});
exports.thing = action(function () {
return exports.Component;
});
exports.arrowWithType = observer(function () {
return "typed";
});
exports.functionWithType = action(function () {
return exports.Component;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//// [tests/cases/compiler/isolatedDeclarationsFunctionExpressionInCall.ts] ////

=== isolatedDeclarationsFunctionExpressionInCall.ts ===
declare function observer<T>(fn: T): T;
>observer : Symbol(observer, Decl(isolatedDeclarationsFunctionExpressionInCall.ts, 0, 0))
>T : Symbol(T, Decl(isolatedDeclarationsFunctionExpressionInCall.ts, 0, 26))
>fn : Symbol(fn, Decl(isolatedDeclarationsFunctionExpressionInCall.ts, 0, 29))
>T : Symbol(T, Decl(isolatedDeclarationsFunctionExpressionInCall.ts, 0, 26))
>T : Symbol(T, Decl(isolatedDeclarationsFunctionExpressionInCall.ts, 0, 26))

declare function action<T>(fn: T): T;
>action : Symbol(action, Decl(isolatedDeclarationsFunctionExpressionInCall.ts, 0, 39))
>T : Symbol(T, Decl(isolatedDeclarationsFunctionExpressionInCall.ts, 1, 24))
>fn : Symbol(fn, Decl(isolatedDeclarationsFunctionExpressionInCall.ts, 1, 27))
>T : Symbol(T, Decl(isolatedDeclarationsFunctionExpressionInCall.ts, 1, 24))
>T : Symbol(T, Decl(isolatedDeclarationsFunctionExpressionInCall.ts, 1, 24))

export const Component = observer(() => {
>Component : Symbol(Component, Decl(isolatedDeclarationsFunctionExpressionInCall.ts, 3, 12))
>observer : Symbol(observer, Decl(isolatedDeclarationsFunctionExpressionInCall.ts, 0, 0))

return "hello";
});

export const thing = action(function () {
>thing : Symbol(thing, Decl(isolatedDeclarationsFunctionExpressionInCall.ts, 7, 12))
>action : Symbol(action, Decl(isolatedDeclarationsFunctionExpressionInCall.ts, 0, 39))

return Component;
>Component : Symbol(Component, Decl(isolatedDeclarationsFunctionExpressionInCall.ts, 3, 12))

});

export const arrowWithType = observer((): string => {
>arrowWithType : Symbol(arrowWithType, Decl(isolatedDeclarationsFunctionExpressionInCall.ts, 11, 12))
>observer : Symbol(observer, Decl(isolatedDeclarationsFunctionExpressionInCall.ts, 0, 0))

return "typed";
});

export const functionWithType = action(function (): typeof Component {
>functionWithType : Symbol(functionWithType, Decl(isolatedDeclarationsFunctionExpressionInCall.ts, 15, 12))
>action : Symbol(action, Decl(isolatedDeclarationsFunctionExpressionInCall.ts, 0, 39))
>Component : Symbol(Component, Decl(isolatedDeclarationsFunctionExpressionInCall.ts, 3, 12))

return Component;
>Component : Symbol(Component, Decl(isolatedDeclarationsFunctionExpressionInCall.ts, 3, 12))

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//// [tests/cases/compiler/isolatedDeclarationsFunctionExpressionInCall.ts] ////

=== isolatedDeclarationsFunctionExpressionInCall.ts ===
declare function observer<T>(fn: T): T;
>observer : <T>(fn: T) => T
> : ^ ^^ ^^ ^^^^^
>fn : T
> : ^

declare function action<T>(fn: T): T;
>action : <T>(fn: T) => T
> : ^ ^^ ^^ ^^^^^
>fn : T
> : ^

export const Component = observer(() => {
>Component : () => "hello"
> : ^^^^^^^^^^^^^
>observer(() => { return "hello";}) : () => "hello"
> : ^^^^^^^^^^^^^
>observer : <T>(fn: T) => T
> : ^ ^^ ^^ ^^^^^
>() => { return "hello";} : () => "hello"
> : ^^^^^^^^^^^^^

return "hello";
>"hello" : "hello"
> : ^^^^^^^

});

export const thing = action(function () {
>thing : () => () => "hello"
> : ^^^^^^^^^^^^^^^^^^^
>action(function () { return Component;}) : () => () => "hello"
> : ^^^^^^^^^^^^^^^^^^^
>action : <T>(fn: T) => T
> : ^ ^^ ^^ ^^^^^
>function () { return Component;} : () => () => "hello"
> : ^^^^^^^^^^^^^^^^^^^

return Component;
>Component : () => "hello"
> : ^^^^^^^^^^^^^

});

export const arrowWithType = observer((): string => {
>arrowWithType : () => string
> : ^^^^^^
>observer((): string => { return "typed";}) : () => string
> : ^^^^^^
>observer : <T>(fn: T) => T
> : ^ ^^ ^^ ^^^^^
>(): string => { return "typed";} : () => string
> : ^^^^^^

return "typed";
>"typed" : "typed"
> : ^^^^^^^

});

export const functionWithType = action(function (): typeof Component {
>functionWithType : () => typeof Component
> : ^^^^^^
>action(function (): typeof Component { return Component;}) : () => typeof Component
> : ^^^^^^
>action : <T>(fn: T) => T
> : ^ ^^ ^^ ^^^^^
>function (): typeof Component { return Component;} : () => typeof Component
> : ^^^^^^
>Component : () => "hello"
> : ^^^^^^^^^^^^^

return Component;
>Component : () => "hello"
> : ^^^^^^^^^^^^^

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// @isolatedDeclarations: true
// @declaration: true

declare function observer<T>(fn: T): T;
declare function action<T>(fn: T): T;

export const Component = observer(() => {
return "hello";
});

export const thing = action(function () {
return Component;
});

export const arrowWithType = observer((): string => {
return "typed";
});

export const functionWithType = action(function (): typeof Component {
return Component;
});