Skip to content

Commit

Permalink
No exact objects with indexers
Browse files Browse the repository at this point in the history
  • Loading branch information
namuol committed Jul 22, 2022
1 parent 6cd728e commit 7f6b6c7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/__tests__/__snapshots__/classes.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ declare class Observable<T> mixins Subscribable<T> {
static +jump?: () => void;
cfnProperties: {
[key: string]: any,
...
};
static fooGet: string;
}
Expand Down
15 changes: 15 additions & 0 deletions src/__tests__/__snapshots__/indexers.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should handle indexers 1`] = `
"declare type Map = {
[key: string]: number,
};
"
`;

exports[`should handle indexers 2`] = `
"declare type Map = {
[key: string]: number,
};
"
`;
24 changes: 24 additions & 0 deletions src/__tests__/indexers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { compiler, beautify } from "..";
import "../test-matchers";

it("should handle indexers", () => {
const ts = `
type Map = {
[key: string]: number
}
`;
{
const result = compiler.compileDefinitionString(ts, { quiet: true });
expect(beautify(result)).toMatchSnapshot();
expect(result).toBeValidFlowTypeDeclarations();
}

{
const result = compiler.compileDefinitionString(ts, {
quiet: true,
inexact: false,
});
expect(beautify(result)).toMatchSnapshot();
expect(result).toBeValidFlowTypeDeclarations();
}
});
20 changes: 19 additions & 1 deletion src/printers/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@ export const interfaceType = <T>(
}
}

if (isType && isInexact) {
const hasIndexSignature = node.members.some(member =>
ts.isIndexSignatureDeclaration(member),
);

// If an index signature is present in our type, we probably don't want to
// provide the trailing "...", since it will be ignored by Flow anyway.
if (isType && isInexact && !hasIndexSignature) {
members.push("...\n");
} else if (members.length > 0) {
members.push("\n");
Expand All @@ -98,6 +104,18 @@ export const interfaceType = <T>(
if (!ts.isTypeLiteralNode(node)) {
return `{${inner}}`;
}

// If a type contains an index signature (e.g. `[key: string]: number`), then
// we want to treat it as inexact no matter what, otherwise we may generate a
// Flow type like `{|[k: string]: any|}` which is invalid in Flow prior to
// v0.126.0 (source: https://github.com/facebook/flow/blob/main/Changelog.md#01260)
//
// It should also be safe to assume that if an index signature is present, the
// equivalent human-authored Flow type would be inexact.
if (hasIndexSignature) {
return `{${inner}}`;
}

return isInexact ? `{${inner}}` : `{|${inner}|}`;
};

Expand Down

0 comments on commit 7f6b6c7

Please sign in to comment.