Никак, респонс в любом случае умеет получать только текст.Это давно неправда: https://developer.mozilla.org/en-US/docs/Web/API/X....
function ExtraCol(props: PriorityColProps): JSX.Element;
function ExtraCol(props: UserColProps): JSX.Element;
function ExtraCol(props: PriorityColProps | UserColProps) {
return (
<div className={props.bWidth}>
<label className="mr-2">{props.labelText}</label>
<b>
{
'priorityId' in props
? <Priority id={props.priorityId} />
: <UserName userId={props.userId} />
}
</b>
</div>
);
<ExtraCol bWidth="col-lg-3" labelText="Priority" priorityId={epic.priority}/>
<ExtraCol bWidth="col-lg-3" labelText="Reporter" userId={epic.reporter} />
function ExtraCol(props: PriorityColProps | UserColProps)
, но это важно для правильно работы типов позже в более сложных случаях. public async getRegDate(
id: number,
config?: Omit<AxiosRequestConfig, 'method' | 'url' | 'responseType'>
): Promise<Date> {
var {data} = await axios.get(`https://vk.com/foaf.php?id=${id}`, {
...config,
responseType: 'document'
});
return new Date(data.querySelector('ya\\:modified').getAttribute('dc:date'));
}
const deviceType = [
{
smartphoneName: 'Precision 5530',
deviceType: 'smartphone',
},
{
smartphoneName: 'MS-7A34',
deviceType: 'computer',
},
{
smartphoneName: 'MS-7A34',
deviceType: 'computer',
},
] as Array<{
smartphoneName: string;
deviceType: DeviceType;
}>
interface IDeviceResponseItem {
smartphoneName: string;
deviceType: DeviceType;
}
function isDeviceResponseItem(item: unknown): item is IDeviceResponseItem {
if (!item)
return false;
const { smartphoneName, deviceType } = item as IDeviceResponseItem;
if (typeof smartphoneName === 'string' && deviceType in DeviceType)
return true;
return false;
}
function getResponse(): IDeviceResponseItem[] {
// ответ от сервера неизвестного типа
const json: unknown = [
{
smartphoneName: 'Precision 5530',
deviceType: 'smartphone',
},
{
smartphoneName: 'MS-7A34',
deviceType: 'computer',
},
{
smartphoneName: 'MS-7A34',
deviceType: 'computer',
},
];
// проверка что ответ именно тот который нужен
if(!Array.isArray(json) || !json.every(isDeviceResponseItem))
throw new Error('[wrong response]');
return json;
}
const deviceType = getResponse();
Array<{
smartphoneName: string;
deviceType: DeviceType;
}>
any
. export enum DeviceType ...
import { DeviceType } from ...
Мне бы как-то только строку получать