Dmitri Pisarev π·πΊ Posted on Apr 14, 2021 Annoying TypeScript discriminating union gotcha #typescript Can you spot what's wrong with this code? https://www.typescriptlang.org/play?ts=4.2.3#code/C4TwDgpgBACgTgezAZygXigbwFBT1AS2QEE44BDEALigDNyAbZCAGl31oDsaAKAN0YBXCDWTA4BTgHMAlOgB8UPggIATbAF8oAHyzs8RUhWpRxwtvjrco-ISKhiJ0gNoBdOWkXK1m7NgDGCJxiUADCCAC2YEEQnMDoNpiGZJQsVho08EjIHoo4loHB8f6MDABG5P4A1gk8uXqWlgS0NsnGcvmNXVw8zgDk5MiqtKp97vqNWhBM0J1dlj19AIwATADMfTITlhoTuxpAA Top comments (2) Subscribe Collapse Expand sevaru sevaru sevaru Follow Joined Dec 31, 2020 • Apr 14 '21 Dropdown menu Copy link Hide TLTR It would work if you write like this const Component = (props: Props) => { const callback = () => { if (props.isArray) { props.fn(['asdfd']) } else { props.fn('123') } } } Enter fullscreen mode Exit fullscreen mode Collapse Expand Dmitri Pisarev π·πΊ Dmitri Pisarev π·πΊ Dmitri Pisarev π·πΊ Follow I'm here to share. Doing stuff for the web since 2005. Mainly ReactJS, React Native & PWAs. Neos CMS team member. Location Moscow, Russia Education MIREA (Moscow technology university) Work Fullstack web developer at Helping nonprofits do good + self-employed Joined May 15, 2020 • Apr 25 '21 Dropdown menu Copy link Hide Yup, exactly! During restructuring TypeScript looses relation between object members. Code of Conduct • Report abuse For further actions, you may consider blocking this person and/or reporting abuse
Top comments (2)
TLTR It would work if you write like this
Yup, exactly! During restructuring TypeScript looses relation between object members.