【发布时间】:2020-06-29 19:40:54
【问题描述】:
我不知道如何输入:这会产生很多错误。
// This seems all wrong as it just creating more errors
interface Response {
status: number;
statusText: string;
error: Error
}
/*
// TS7053: Element implicitly has an 'any' type because expression
of type '"text" | "json"' can't be used to index type 'Response'. Property 'text' does not exist on type 'Response'
*/
function parseResponse(response) {
return response[response.status === 204 ? 'text' : 'json']();
}
function checkStatus(response) {
if (response && response.status >= 200 && response.status < 300) {
return response;
}
const error = new Error(response ? response.statusText : 'Offline');
error.response = response; // TS2339: Property 'response' does not exist on type 'Error'.
throw error;
}
我还有其他问题吗?
export default function request(url: string, options = {}) {
const requestOptions = {
credentials: 'include',
headers: {
Accept: 'application/json',
},
};
const tokenAuth = authToken.getAuthToken();
if (tokenAuth) {
requestOptions.headers['Auth-Token'] = tokenAuth;
}
显示错误:TS7053:元素隐式具有“任何”类型,因为“Auth-Token”类型的表达式不能用于索引类型“{ Accept: string; }'。类型 '{ Accept: string; 上不存在属性 'Auth-Token' }'。
【问题讨论】:
标签: typescript