尝试将window.location.href设置为从接口返回的对象

问题描述 投票:0回答:1

我收到此错误

Type MyInterface' is not assignable to type 'string'
。我明白了,但想不出好的解决办法。
MyInterface
仅返回一项,即字符串。这是运行的代码。

export interface MyInterface {
  pulledCoolData: string;
}

const getData = async (passedData: string): Promise<MyInterface> => {
  const coolData = await getDataFromAPI(passedData);

  return coolData;
};


getData(data.myData).then(response => {
    window.location.href = response;//the error is on this line because reponse is type MyInterface and it is expecting a string to be passed
});
javascript reactjs typescript interface
1个回答
0
投票

我想你忘了从

pulledCoolData
获取
MyInterface
:

getData(data.myData).then(response => {
    window.location.href = response.pulledCoolData;
});

不过,我没有 TypeScript 经验,所以这可能是不正确的。

© www.soinside.com 2019 - 2024. All rights reserved.