Skip to content

Commit

Permalink
Fix omitting manual type for arrow functions
Browse files Browse the repository at this point in the history
Summary:
Changelog:
[flow-api-translator] - Fixed omitting manual type for arrow functions

Introduced in the previous arrow functions diff (D68160295) the manual type is omitted and void is generated as the return type instead.

Given subsequent JS code:

```js
export class Foo {
    foo: (val: string) => number = (val: string) => {
       return 1;
    };
}
```

the flowToFlowDef should generate:

```js
export declare class Foo {
    foo: (val: string) => number;
}
```

This is the current output (before the diff):

```js
export declare class Foo {
    foo: (val: string) => void;
}
```

Reviewed By: pieterv

Differential Revision: D69923039

fbshipit-source-id: 5a71102889fcc235ce4cb36b8c50435964c31109
  • Loading branch information
coado authored and facebook-github-bot committed Feb 25, 2025
1 parent 038bfdb commit 03ef8e1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,26 @@ describe('flowToFlowDef', () => {
static foo: () => void;
}`,
);
await expectTranslate(
`export class A {
foo: (val: string) => number = (val: string) => { return 1 };
static foo: (val: string) => number = (val: string) => { return 1 };
}`,
`declare export class A {
foo: (val: string) => number;
static foo: (val: string) => number;
}`,
);
await expectTranslate(
`export class A {
foo = (val: string): number => { return 1 };
static foo = (val: string): number => { return 1 };
}`,
`declare export class A {
foo: (val: string) => number;
static foo: (val: string) => number;
}`,
);
});
it('method', async () => {
await expectTranslate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,10 @@ function convertClassMember(
);
}

if (member.value?.type === 'ArrowFunctionExpression') {
if (
member.value?.type === 'ArrowFunctionExpression' &&
member.typeAnnotation == null
) {
const [resultTypeAnnotation, deps] = convertAFunction(
member.value,
context,
Expand Down

0 comments on commit 03ef8e1

Please sign in to comment.