Typescript: Access window object with type check
2019-11-15
"typescript": "3.7.2"
We have an external html provide window.configs
.
We may need to access window
object in typescript to get config properties, and do validation before initial async request.
windowConfig.tsx
:
interface iConfig extends Window { config: { username: string, server: string, port: number, } } declare var window: iConfig; export const windowConfig = window;
index.tsx
:
import { windowConfig } from './windowConfig'; /* use windowConfig as 'window' */
import { useEffect } from 'react'; /* Add declaration for window.AMap */ declare let AMap; export default function Map() { let map; useEffect(() => { /* Directly use AMap */ map = new AMap.Map(`container`, { zoom: 10, }); }, []); return ( /* ... */ ); }
关于本文
文章标题 | Typescript: Access window object with type check |
发布日期 | 2019-11-15 |
文章分类 | Tech |
相关标签 | #Typescript #Javascript #Config |
留言板
PLACE_HOLDER
PLACE_HOLDER
PLACE_HOLDER
PLACE_HOLDER