Guest User

Untitled

a guest
Nov 19th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. type Opt = { id: string, name: string }
  2.  
  3. interface MultiProps {
  4. isMultiple: true;
  5. options: Opt[];
  6. id: string[];
  7. onChange: (id: string[]) => void;
  8. }
  9.  
  10. interface SingleProps {
  11. isMultiple: false;
  12. options: Opt[];
  13. id: string;
  14. onChange: (id: string) => void;
  15. }
  16.  
  17. type SelectProps = MultiProps | SingleProps;
  18.  
  19. function Select(props: SelectProps) {
  20. if (props.isMultiple) {
  21. // works
  22. const { id, onChange } = props;
  23. onChange(id);
  24. } else {
  25. // fail
  26. const { id, onChange } = props;
  27. onChange(id); // error here
  28. }
  29. }
  30.  
  31. let m: MultiProps = {
  32. isMultiple: true,
  33. options: [{ id: 'a', name: 'A' }],
  34. id: ['a'],
  35. onChange: null
  36. };
  37.  
  38. let s: SingleProps = {
  39. isMultiple: false,
  40. options: [{ id: 'a', name: 'A' }],
  41. id: 'a',
  42. onChange: null
  43. };
  44.  
  45. Select(m);
  46. Select(s);
Add Comment
Please, Sign In to add comment