This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const makeBigger = (stringOrNumber: string | number) => { | |
if (typeof stringOrNumber === 'string') { | |
return stringOrNumber.toUpperCase(); | |
} | |
return stringOrNumber * 4; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Nullable<IdType, RequiredType> = RequiredType extends true | |
? IdType | |
: IdType | null; | |
const Select = <IdType extends string, RequiredType extends boolean>(props: { | |
options: Array<{id: IdType; name: string}>; | |
selectedItemId: Nullable<IdType, RequiredType>; | |
onSelect: (id: Nullable<IdType, RequiredType>) => void; | |
required?: RequiredType; | |
}) => ( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type BaseEntityWithFancyId<IdType> = { | |
id: IdType; | |
name: string; | |
}; | |
const Select = <IdType extends string>(props: { | |
options: Array<BaseEntityWithFancyId<IdType>>; | |
selectedItemId?: IdType; | |
onSelect: (id: IdType) => void; | |
}) => ( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Select = <IdType extends string>(props: { | |
options: Array<{ | |
id: IdType; | |
name: string; | |
}>; | |
selectedItemId?: IdType; | |
onSelect: (id: IdType) => void; | |
}) => ( | |
<select | |
value={props.selectedItemId ?? ''} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Select | |
onSelect={id => { | |
// Do something with id | |
}} | |
options={[ | |
{id: Size.Small, name: 'Small'}, | |
{id: Size.Medium, name: 'Medium'}, | |
{id: Size.Large, name: 'Large'}, | |
]} | |
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Select<Size> | |
onSelect={id => { | |
// Do something with id | |
}} | |
> | |
<option value={Size.Small}>Small</option> | |
<option value={Size.Medium}>Medium</option> | |
<option value={Size.Large}>Large</option> | |
</Select> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Select | |
onSelect={id => { | |
// Do something with id | |
}} | |
> | |
<option value={Size.Small}>Small</option> | |
<option value={Size.Medium}>Medium</option> | |
<option value={Size.Large}>Large</option> | |
</Select> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const List = <ItemType extends {id: string}>(props: { | |
items: ItemType[]; | |
renderItem: (item: ItemType) => React.ReactNode; | |
}) => ( | |
<ul> | |
{props.items.map(item => ( | |
<li key={item.id}>{props.renderItem(item)}</li> | |
))} | |
</ul> | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const List = <ItemType extends any>(props: { | |
items: ItemType[]; | |
renderItem: (item: ItemType) => React.ReactNode}; | |
) => ( | |
<ul> | |
{props.items.map(item => ( | |
<li key={item.id}>{props.renderItem(item)}</li> | |
))} | |
</ul> | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const List = <ItemType extends any>(props: { | |
items: ItemType[]; | |
renderItem: (item: ItemType) => React.ReactNode; | |
}) => ( | |
<>{props.items.map(props.renderItem)}</> | |
); |