Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type Opt = { id: string, name: string }
- interface MultiProps {
- isMultiple: true;
- options: Opt[];
- id: string[];
- onChange: (id: string[]) => void;
- }
- interface SingleProps {
- isMultiple: false;
- options: Opt[];
- id: string;
- onChange: (id: string) => void;
- }
- type SelectProps = MultiProps | SingleProps;
- function Select(props: SelectProps) {
- if (props.isMultiple) {
- // works
- const { id, onChange } = props;
- onChange(id);
- } else {
- // fail
- const { id, onChange } = props;
- onChange(id); // error here
- }
- }
- let m: MultiProps = {
- isMultiple: true,
- options: [{ id: 'a', name: 'A' }],
- id: ['a'],
- onChange: null
- };
- let s: SingleProps = {
- isMultiple: false,
- options: [{ id: 'a', name: 'A' }],
- id: 'a',
- onChange: null
- };
- Select(m);
- Select(s);
Add Comment
Please, Sign In to add comment