Reactjs 如何在typescript中将命名空间引用为类型
Reactjs 如何在typescript中将命名空间引用为类型,reactjs,typescript,google-maps,Reactjs,Typescript,Google Maps,google-map-react为我加载google-map库。但加载的库没有类型信息。 它只是将其作为any返回,如下所示 function onLoadGoogleMap(): { maps: any } { return { maps: google.maps } } const googleMaps: { maps: typeof google.maps } = { maps: maps, } 所以我自己安装了@types/google
google-map-react
为我加载google-map库。但加载的库没有类型信息。它只是将其作为
any
返回,如下所示
function onLoadGoogleMap(): { maps: any } {
return {
maps: google.maps
}
}
const googleMaps: {
maps: typeof google.maps
} = {
maps: maps,
}
所以我自己安装了@types/googlemaps
。然后我想像这样设置它的类型信息。但是它给了我一个错误,名称空间“google”没有导出的成员“maps”。
function main() {
// maps is loaded as any
const { maps } = onLoadGoogleMap()
// I want type information, BUT this show me a error.
const googleMaps: {
maps: google.maps
} = {
maps: maps,
}
new googleMaps.maps.LatLng(12, 32)
}
我找到了@types/googlemaps
的样子。和这个差不多
declare namespace google.maps {
class LatLng {
constructor(lat: number, lng: number)
}
}
如何设置命名空间的类型信息?操场在这里
谢谢你的阅读。我找到了解决这个问题的方法
@types/googlemaps
是一组定义,但typescript中的名称空间更像对象。了解typescript中的名称空间是一个很好的来源 因此,我们可以使用
typeof
关键字引用名称空间类型,如下所示
function onLoadGoogleMap(): { maps: any } {
return {
maps: google.maps
}
}
const googleMaps: {
maps: typeof google.maps
} = {
maps: maps,
}
我找到了解决这个问题的方法
@types/googlemaps
是一组定义,但typescript中的名称空间更像对象。了解typescript中的名称空间是一个很好的来源 因此,我们可以使用
typeof
关键字引用名称空间类型,如下所示
function onLoadGoogleMap(): { maps: any } {
return {
maps: google.maps
}
}
const googleMaps: {
maps: typeof google.maps
} = {
maps: maps,
}