Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/complete-indexed-type-details.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-docgen': patch
---

Preserve union members and nested type details when resolving TypeScript indexed access properties.
30 changes: 30 additions & 0 deletions packages/react-docgen/src/utils/__tests__/getTSType-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,36 @@ describe('getTSType', () => {
expect(getTSType(typePath)).toMatchSnapshot();
});

test('preserves union members in indexed access', () => {
const typePath = typeAlias(`
var x: Base["size"];
type Base = { size: "small" | "large" };
`);

expect(getTSType(typePath)).toMatchObject({
name: 'union',
raw: 'Base["size"]',
elements: [
{ name: 'literal', value: '"small"' },
{ name: 'literal', value: '"large"' },
],
});
});

test('preserves object signatures in indexed access', () => {
const typePath = typeAlias(`
var x: Base["options"];
type Base = { options: { enabled: boolean } };
`);

expect(getTSType(typePath)).toMatchObject({
name: 'signature',
signature: {
properties: [{ key: 'enabled', value: { name: 'boolean' } }],
},
});
});

test('resolves indexed access of array', () => {
const typePath = parseTypescript
.statement(
Expand Down
10 changes: 9 additions & 1 deletion packages/react-docgen/src/utils/getTSType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,16 @@ function handleTSIndexedAccessType(
return { name: 'unknown' };
}

const valueType: TypeDescriptor<TSFunctionSignatureType> & {
required?: boolean;
} = {
...resolvedType.value,
};

delete valueType.required;

return {
name: resolvedType.value.name,
...valueType,
raw: printValue(path),
};
}
Expand Down