我正在使用Axios进行API调用,并返回JSON。API返回CUSIP作为字符串类型,然而,我希望将其作为数字类型接收。我创建了一个接口,其中typescript类型为number,但是当我获取变量时,它仍然被视为字符串。
API调用和一些逻辑:
const axios = require('axios');
import { General } from './json-objects-new';
module.exports = {
makeApiCall : function(ticker:string) {
axios.get(`${API_ENDPOINT}${ticker}?api_token=${API_KEY}`)
.then(function (response) {
// handle success
return response.data;
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(data => {
let gen : General = data.General;
let num = gen.CUSIP + 1337
console.log(num);
});
}
}
我创建了一个名为General的接口,在其中将CUSIP转换为数字:
export interface General {
ISIN: string;
CUSIP: number;
}
问题:输出应该是 [CUSIP + 1337] = [2 + 1337 = 1339],但实际输出的是[21337]。希望得到一些帮助,谢谢。我真的不想在构造函数中手动转换所有内容。