kHRISl33t / runtime-env-cra

Runtime environment handler for create-react-apps
MIT License
49 stars 23 forks source link

"Property does not exist on type Window in TypeScript" #21

Closed Jamal8548 closed 2 years ago

Jamal8548 commented 2 years ago

I was following the steps which are mentioned in the section usage with typescript but it was not working there and throwing error "Property does not exist on type Window in TypeScript" but i found solution to this problem for future users if by chance they would face the same situation..

SOLUTION for problem "Property does not exist on type Window in TypeScript"

STEP:1 In your src directory, create a types directory that contains the following index.d.ts file: src/types/index.d.ts

export {};

declare global { interface Window { __RUNTIME_CONFIG__: { NODE_ENV: string; REACT_APP_API_URL: string; }; } }

STEP: 2 add the path to your types directory to your tsconfig.json file.

tsconfig.json

{ "compilerOptions": { // ... rest "typeRoots": ["./node_modules/@types", "./src/types"] } } STEP: 3 Now use it, whereever you want to do it.

{window.__RUNTIME_CONFIG__.NODE_ENV}