我正在尝试使用 fetch 和 typescript 创建发布请求。但无法创建 204 状态处理程序。
我已经尝试用空值返回承诺,但它不起作用。
postRequest = async <T>(url: string, body: any): Promise<T> => {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
body: JSON.stringify(body)
});
// here is my problem
if (response.status === 204) {
// error is: "Type 'null' is not assignable to type 'T'."
return null;
// I have tried to return promise, but it doesn't work.
// error is: "Argument of type 'null' is not assignable to
// …Run Code Online (Sandbox Code Playgroud) 此代码适用于 IOS,但不适用于 Android(模拟器/真实设备)
<MapView region={this.state.region}>
<UrlTile
urlTemplate="http://c.tile.openstreetmap.org/{z}/{x}/{y}.png"
maximumZ={19}
zIndex={99}
/>
</MapView>
Run Code Online (Sandbox Code Playgroud)
不明白问题出在哪里
UrlTile可以正常使用此 url http://tile.stamen.com/terrain/{z}/{x}/{y}.jpg,但不能使用osmurl,http://c.tile.openstreetmap.org/{z}/{x}/{y}.png尽管osmurl 在浏览器中可以工作
当我交换对象键值时,函数的返回类型是 { [x: string]: value } instedof const key。
游乐场在这里
interface IObj {
[key: string]: string;
};
const swapFn = <T extends IObj>(obj: T) => {
const res = {} as { [K in T[keyof T]]: keyof T };
Object.entries(obj).forEach(([key, value]) => {
res[value as T[keyof T]] = key;
});
return res;
}
// return type should be:
// {
// aaa: 'a',
// bbb: 'b',
// } insted of {
// [x: string]: "a" | "b";
// } …Run Code Online (Sandbox Code Playgroud)