score:0
Apart from the suggested solutions, you may also use discriminating unions: link to the doc which is designed specifically to solve this kind of situations (refer the doc).
like this:
type DogProps = {
animalName: "dog";
bark: () => void;
}
type FishProps = {
animalName: "fish";
swim: () => void;
}
type AnimalProps = DogProps | FishProps;
const Animal = (props: AnimalProps) => {
if(props.animalName === 'dog') {
props.bark();
} else {
props.swim();
}
}
TS Playground link: https://tsplay.dev/NddryN
Destructing isn't possible until you narrow down the type like this (you see other props resolve appropriately without hassle of checking):
type DogProps = {
animalName: "dog";
bark: () => void;
dogProp: string;
}
type FishProps = {
animalName: "fish";
swim: () => void;
fishProp: number;
}
type AnimalProps = DogProps | FishProps;
const Animal1 = (props: AnimalProps) => {
if(props.animalName === 'dog') {
const { bark, dogProp } = props;
bark();
} else {
const {swim, fishProp} = props;
swim();
}
}
see this: https://tsplay.dev/N9pn7w
score:0
You can simply check if the function
exists before calling it.
if (animalName === 'dog' && typeof bark === 'function') {
bark();
}
If you feel like that is too verbose, you can also take advantage of optional-chaining to do the same thing.
if (animalName === 'dog') {
bark?.(); // Will only invoke bark() if it's defined
}
Optional-chaining is supported in TypeScript since version 3.7.
Source: stackoverflow.com
Related Query
- How to conditionally call different optional function props in a React TypeScript component without getting compiler error?
- How to handle typescript function overloading with React Props
- How to declare different state and props for subclasses on react with typescript
- How to conditionally pass optional props to component when we use React with Typescript?
- How to make a react component call a function from another Material UI Typescript
- How to pass a function through props in React to a function in a different component?
- How to call a function defined inside react component in a typescript class?
- How to call props arrow function in react
- How to call a function from different component using React with Redux Form?
- How to call a function in another component from a different component in react js?
- How do I send a function through props in react typescript
- How to pass data from a long fetch call function as props in React
- How can a function correctly accept a React component and its props as arguments in TypeScript and render this component?
- How to make a button onClick in one component call a function in a different component React
- how to declare a react ref in props function when using typescript
- How to call jQuery function from React typescript component?
- React - How can I force call a props function
- How to call loading function with React useEffect only once
- How to specify (optional) default props with TypeScript for stateless, functional React components?
- React prop types with TypeScript - how to have a function type?
- Typescript React: Conditionally optional props based on the type of another prop
- How to call a function every minute in a React component?
- How to extend a native's component's props TypeScript interface in a stateless, functional component in React Native?
- How to use jest.spyOn with React function component using Typescript
- React Typescript how send props and use it in child component
- React - How do you call a function from inside another function
- how to conditionally add props on a react component?
- Spread operator with React Props in Typescript (error: ...could be instantiated with a different subtype of constraint {})
- How to call a component function on other component, but from the other component ? React
- How to avoid redeclaring props type in React constructor using Typescript
More Query from same tag
- React: disable a filtered item
- Typescript not allowing to pass props to children with React-notification-component {children?: ReactNode;}
- Pass state from one reducer to another in Redux
- problems while using array.map for null entry in react
- How do I render html tags (or new lines) within a variable?
- onMouseEnter and onMouseLeave to animate multiple Lottie icons in React
- React Variable value is not changing
- Component not updating on deeply nested redux object
- How can I stop the execution of my promise based on the boolean value of my prop?
- Next.js toggle display of a div tag
- React.js: Issue passing a functional prop within same class
- React with Typescript | Functional component | Type 'boolean' is not assignable to type 'ReactElement<any, any>'
- ReactJS integration with libraries using getElementById
- How can I force a component to re-render with hooks in React?
- React setState with multiple AJAX calls
- this.refs is undefined for shallow tests using enzyme on React native
- Change in state not being rendered with object.map
- Deployed a website in React using Netlify, but I get a blank page
- Trying to render an JSON dict received from elastic-search but getting [object Promise] error
- How to load image in asynchronous manner on hover effect in React.js?
- Why is my URL [object%20Object] in API call?
- React Dropzone - async function in onDrop callback doesn't update state
- PropTypes error while Passing array as props
- Pass prop value down from login component to routes component
- How made a deep copy that can resolve a problem with a table
- Dynamically Creating Table Rows With React
- issue with passing hook to child typescript
- Error while splitting Reducer
- addEventListener on refs in react showing strange behaviour
- Give className to the matched element in forEach loop with React.js