Access window object with type check
文章目录

Typescript: Access window object with type check

Backgrounds

1
"typescript": "3.7.2"

Requirements

An external html to set window.configs.

So we may need to access window object in typescript to get config properties, and do some validation before use

windowConfig.tsx:

1
2
3
4
5
6
7
8
9
10
11
interface iConfig extends Window {
config: {
username: string,
server: string,
port: number,
}
}

declare var window: iConfig;

export const windowConfig = window;

index.tsx:

1
2
3
import { windowConfig } from './windowConfig';

/* use windowConfig as 'window' */