0

I have the following ES6 class.

class Http {
    isFormData;
    url;
    token;
    data;

    constructor() {
        this.isFormData = false;
        this.url = '';
        this.token = '';
        this.data = {};
    }

    /** Set url */
    setUrl(url: string) {
        this.url = url;
        return this;
    }

    /** Set data */
      setData(data: Object) {
        this.data = data;
        return this;
    }
}

export default new Http();

I want to make it a typescript class. I tried but some properties are not identified properly. How can I make it a typescript class?

2 Answers 2

5

You need to specify the types of your properties. I also suggest you make them private:

class Http {
    private isFormData: boolean;
    private url: string;
    private token: string;
    private data: Object;

    constructor() {
        this.isFormData = false;
        this.url = '';
        this.token = '';
        this.data = {};
    }

    /** Set url */
    setUrl(url: string) {
        this.url = url;
        return this;
    }

    /** Set data */
    setData(data: Object) {
        this.data = data;
        return this;
    }
}

export default new Http();
Sign up to request clarification or add additional context in comments.

Comments

0

its better to export your class with export class Http so you can import it as import {Http} from './http'

 export  class Http {
 isFormData: boolean;
 url: string;
 token: string;
 data: Object;

constructor() {
    this.isFormData = false;
    this.url = '';
    this.token = '';
    this.data = {};
}

/** Set url */
setUrl(url: string) {
    this.url = url;
    return this;
}
/** Set data */
setData(data: Object) {
    this.data = data;
    return this;
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.