TypeScript Version: 2.9.2
(also tried typescript@3.1.0-dev.20180727 )
Search Terms: React Tsx Union Generics
Code
interface PS {
multi: false
value: string | undefined
onChange: (selection: string | undefined) => void
}
interface PM {
multi: true
value: string[]
onChange: (selection: string[]) => void
}
export function ComponentWithUnion(props: PM | PS) {
return "foo";
}
// Usage with React tsx
export function HereIsTheError() {
return (
<ComponentWithUnion
multi={false}
value={'s'}
onChange={val => console.log(val)} // <- this throws an error
/>
);
}
// Usage with pure TypeScript
ComponentWithUnion({
multi: false,
value: 's',
onChange: val => console.log(val) // <- this works fine
})
Expected behavior:
Both should infer the correct type and don't throw an error.
Actual behavior:
Throw's the following error:
Parameter 'val' implicitly has an 'any' type
Playground Link:
https://codesandbox.io/s/w07omnr85k
In file index.ts at line 25 you can see that val is of type any. But in line 34 it correctly displays type string.
Related Issues:
None
TypeScript Version: 2.9.2
(also tried typescript@3.1.0-dev.20180727 )
Search Terms: React Tsx Union Generics
Code
Expected behavior:
Both should infer the correct type and don't throw an error.
Actual behavior:
Throw's the following error:
Playground Link:
https://codesandbox.io/s/w07omnr85k
In file index.ts at line 25 you can see that
valis of typeany. But in line 34 it correctly displays typestring.Related Issues:
None