小编Art*_*rev的帖子

如何使用 Typescript 处理 204 状态并获取?

我正在尝试使用 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)

javascript fetch typescript

5
推荐指数
1
解决办法
2059
查看次数

React Native 地图 UrlTile 无法与 Android 上的 openstreetmap url 配合使用

此代码适用于 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 在浏览器中可以工作

安卓: 安卓 苹果系统: IOS

openstreetmap react-native react-native-maps

5
推荐指数
1
解决办法
1987
查看次数

如何交换对象键值并获得交换函数的有效返回类型?

当我交换对象键值时,函数的返回类型是 { [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)

typescript typescript-generics

3
推荐指数
1
解决办法
2290
查看次数