简体   繁体   中英

Creating (my own) TypeScript Library

Following the tutorial on this page I have the following JavaScript:

function A(id) {
    var about = {
        Version: "0.0.0.1",
    };

    if (id) {
        if (window === this) {
            return new A(id);//<-Error in typescript
        }

        this.e = document.getElementById(id);
        return this;
    } else {
        return about;
    }
};

A.prototype = {
    doSomething: function () {
        //Some logic
        return this;
    }
}

JavaScript Usage:

var result = A("test");//The type of result should be 'A'
result = new A("test");//The type of result should be 'A'
result = A("test").doSomething();//The type of result should be 'A'
result = new A("test").doSomething();//The type of result should be 'A'

I want to create a library based on TypeScript that has the same usage. How can that be done?

If I try to just put this in a '.ts' file and compile I get an error saying: Error TS2350: Only a void function can be called with the 'new' keyword.

Also I am not sure how the usage with and without the 'new' keyword can be achieved as I am trying to make a library in typescript that can be used in javascript and not to make the user have to use the 'new' keyword.

PS
I know I can create a javascript file with a '.d.ts' file but that is not the purpose.

TypeScript is trying to reconcile the multiple purposes of A() and doesn't have enough information to figure out what you want it to look like.

This works :

function A(id): void {
    if (window === this) {
        return new A(id);//<-Error in typescript
    }

    this.e = document.getElementById(id);
    return this;
};

let result = A("test");
result = new A("test").doSomething();

But adding the version return block is confusing the compiler because A is now acting like two totally different things.

If you want to force the compiler to ignore the complaint you can do this:

function A(id): void {
    var about = {
        Version: "0.0.0.1",
    };

    if (id) {
        if (window === this) {
            return new A(id);//<-Error in typescript
        }

        this.e = document.getElementById(id);
        return this;
    } else {
        return <any>about;
    }
};

let result = A("test");
result = new A("test").doSomething();

But I don't recommend it. What you are trying to do is part of what TypeScript is trying to protect against. I don't know of a way to make the typings on this one "just work".


Edit: In response to your comment, I recommend something more like this:

class A {
    public static about = {
        Version: "0.0.0.1"
    };

    private e: HTMLElement;

    constructor(id) {
        this.e = document.getElementById(id);
    }

    public doSomething() {
        //Some logic
        return this;
    }
}

let result = new A("test");
result.doSomething();

let about = A.about;

This is the final code I came up with thanks to MrHen help:

class A {
    public element;
    public test;
    public doSomething;

    constructor(id) {
        if (id) {
            if (window === this as any) {
                return new A(id);//<-Error in typescript
            }

            this.element = document.getElementById(id);
            this.test = function () {
                console.log("testing"); return this;
            };
            this.doSomething = function () {
                console.log("doing something"); return this;
            };

            return this;
        }
    }
}

let result = ((A as any)("test") as A).doSomething();
result = new A("test").doSomething();

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