Skip to content

Commit 56b0f86

Browse files
authored
Merge pull request #2111 from Shopify/dangit-typescript
Fix issue where TS couldn't infer types for things with subcomponents
2 parents d0c076d + ed8d4de commit 56b0f86

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

UNRELEASED.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Use [the changelog guidelines](https://git.io/polaris-changelog-guidelines) to f
1212

1313
### Bug fixes
1414

15+
- Worked around issue where TypeScript will not generate correct types for functional components that have subcomponents ([#2111](https://github.com/Shopify/polaris-react/pull/2111))
16+
1517
### Documentation
1618

1719
### Development workflow

src/components/Autocomplete/Autocomplete.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@ export interface AutocompleteProps {
3838
onLoadMoreResults?(): void;
3939
}
4040

41-
export function Autocomplete({
41+
// TypeScript can't generate types that correctly infer the typing of
42+
// subcomponents so explicitly state the subcomponents in the type definition.
43+
// Letting this be implicit works in this project but fails in projects that use
44+
// generated *.d.ts files.
45+
46+
export const Autocomplete: React.FunctionComponent<AutocompleteProps> & {
47+
ComboBox: typeof ComboBox;
48+
TextField: typeof TextField;
49+
} = function Autocomplete({
4250
id,
4351
options,
4452
selected,
@@ -86,7 +94,7 @@ export function Autocomplete({
8694
emptyState={emptyState}
8795
/>
8896
);
89-
}
97+
};
9098

91-
Autocomplete.TextField = TextField;
9299
Autocomplete.ComboBox = ComboBox;
100+
Autocomplete.TextField = TextField;

src/components/Card/Card.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,16 @@ export interface CardProps {
3131
secondaryFooterActionsDisclosureText?: string;
3232
}
3333

34-
export function Card({
34+
// TypeScript can't generate types that correctly infer the typing of
35+
// subcomponents so explicitly state the subcomponents in the type definition.
36+
// Letting this be implicit works in this project but fails in projects that use
37+
// generated *.d.ts files.
38+
39+
export const Card: React.FunctionComponent<CardProps> & {
40+
Header: typeof Header;
41+
Section: typeof Section;
42+
Subsection: typeof Subsection;
43+
} = function Card({
3544
children,
3645
title,
3746
subdued,
@@ -102,8 +111,8 @@ export function Card({
102111
</div>
103112
</WithinContentContext.Provider>
104113
);
105-
}
114+
};
106115

107-
Card.Section = Section;
108116
Card.Header = Header;
117+
Card.Section = Section;
109118
Card.Subsection = Subsection;

src/components/TopBar/TopBar.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,16 @@ export interface TopBarProps {
3333
onNavigationToggle?(): void;
3434
}
3535

36-
export function TopBar({
36+
// TypeScript can't generate types that correctly infer the typing of
37+
// subcomponents so explicitly state the subcomponents in the type definition.
38+
// Letting this be implicit works in this project but fails in projects that use
39+
// generated *.d.ts files.
40+
41+
export const TopBar: React.FunctionComponent<TopBarProps> & {
42+
Menu: typeof Menu;
43+
SearchField: typeof SearchField;
44+
UserMenu: typeof UserMenu;
45+
} = function TopBar({
3746
showNavigationToggle,
3847
userMenu,
3948
searchResults,
@@ -123,8 +132,8 @@ export function TopBar({
123132
</div>
124133
</div>
125134
);
126-
}
135+
};
127136

128-
TopBar.UserMenu = UserMenu;
129-
TopBar.SearchField = SearchField;
130137
TopBar.Menu = Menu;
138+
TopBar.SearchField = SearchField;
139+
TopBar.UserMenu = UserMenu;

0 commit comments

Comments
 (0)