简体   繁体   中英

Property 'location' does not exist on type 'typeof window'

My code was working until I upgraded the typescript and react frameworks.This error is happening on the line window.location.assign("#/home/dashboard"); . I have read articles but still cannot resolve the issue. I also think the window property is from modernizer.js. Currently, the error I have been getting when I try to run the project is below:

TS2339: Property 'location' does not exist on type 'typeof window'.

This is the code below

import RaisedButton from "material-ui/RaisedButton";
import {ActionLockOutline, SocialPerson} from "material-ui/svg-icons";
import * as React from "react";
import {hot} from "react-hot-loader";
import { OAuth } from "../../Shared/Services/OAuth";
import { Component, IComponentState } from "../../Shared/Utilities/Component";
import { Log } from "../../Shared/Utilities/LogUtil";
import { Validation } from "../../Shared/Utilities/Validation";

// import { StorageUtil } from "../../Shared/Utilities/StorageUtil";

interface ILoginState extends IComponentState {
    userName: string;
    password: string;
}

class Form extends Component<any, ILoginState> {

    constructor(props: any) {
        super(props,
            {
                password: "",
                userName: "",
            });

    }

    public loginClick = () => {
        // this.warningNoti("All fields marked red are required");

        if (Validation.formValidation("#loginForm")) {
            OAuth.userLogin(this.state.userName, this.state.password, (loginResponse: any) => {
                Log.consoleObj(loginResponse);

                this.successNoti("User successfully logged in");

                window.location.assign("#/home/dashboard");

            }, (status: string, jqXhr: any) => {
                // console.log(status);
                // console.log(jqXhr);
                this.setState({password: ""});
                this.infoNoti("Incorrect username or password, please check credentials.");
            });
        } else {
            this.warningNoti("All fields marked red are required");
        }

    }
}

First Answering your question: Why tslint raises that "Property 'location' does not exist on type 'window'. It is because Typescript has some set of types defined, default types and custom types. For typescript window is custom type. Default types are number, string,boolean,any,etc. So when you want to use window, it is treated as a window object which has functions like assign, location and so on defined as interface in default lib.dom.d.ts. For now I suggest a work around for you by creating a const with type set as any. reference: https://www.typescriptlang.org/docs/handbook/basic-types.html

Solution/Workaround: set type of your constant by yourself by doing this :

const temp: any = window;
temp.location.assign('#/home/dashboard');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM