Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

TypeScript Type Assertion in JSX

$
0
0

How can I assert that the profile is UserProfileForAdmin type only inside the isAdmin JSX area to display the phone number only for the admins.

import { View, Text } from "react-native";interface Profile {  name: string;}interface UserProfileForAdmin extends Profile {  phone: string;}export interface HomeSingleProfileProps {  profile: Profile | UserProfileForAdmin;  isAdmin: boolean;}const SingleProfile = (props: HomeSingleProfileProps) => {  return (<View><Text>        {props.isAdmin ? (<>            {props.profile.phone}</>        ) : (<>            {props.profile.name}</>        )}</Text></View>  );};export default SingleProfile;

enter image description here

Currently it gives the error

TS2339: Property 'phone' does not exist on type 'Profile | UserProfileForAdmin'.   Property 'phone' does not exist on type 'Profile'.


Viewing all articles
Browse latest Browse all 6287

Trending Articles