0

I am trying to implement a generic discrete interval encoding tree with typescript. Generic here means I want to allow interval endpoints to be arbitrary classes that extends a simple interface specifying discrete linear orders which enforces the existence of two method members next and lessThan. Because I want to type the methods I assume I need a generic interface (all the code can be found in this jsfiddle):

interface DiscreteLinearOrder<T> {
    next = () => T;
    lessThan = (y: T) => Boolean;
}

Then I instantiated this with number (ok, it is not discrete, but think of it as integers ;-)

class DLOnumber implements DiscreteLinearOrder<number> {
    private value: number;
    constructor(x: number) { this.value = x; }
    next = () => { return(this.value + 1); };
    lessThan = (y: number) => { return(this.value < y); };
    getValue = () => { return(this.value.toString()); }
}

Up to here it worked, and running things like

const bar = new DLOnumber(5);

worked out.

Having this I planed to provide a class DLOinterval that takes two parameters, one extending the discrete linear order interface:

class DLOinterval<T, U extends DiscreteLinearOrder<T>> {
    private start: U;
    private end: U;
    private data: any;
    constructor(s: U, e: U, d: any) {
        this.start = s;
        this.end   = e;
        this.data  = d;
    }
}

But here the troubles are starting: I cannot define a type alias and use it:

type NumberInterval = DLOinterval<number, DLOnumber>;
const ggg = new NumberInterval(new DLOnumber(3),new DLOnumber(5),null)

neither can I instantiate it directly:

const aaa = new DLOinterval<number, DLOnumber>(new DLOnumber(3),new DLOnumber(5),null)

What am I missing here?

6
  • When you define NumberInterval as type NumberInterval = DLOInterval<number, DLOnumber>;, you are defining a type, not a class constructor, so I don't believe you should be calling new NumberInterval(...). Also, note that, in type NumberInterval = DLOInterval<number, DLOnumber>;, DLOinterval should use a lowercase 'i' (based on your class constructor). Commented Apr 6, 2018 at 2:40
  • Thanks, fixed that. Commented Apr 6, 2018 at 2:51
  • 1
    This open issue may be relevant here: github.com/Microsoft/TypeScript/issues/1213 Commented Apr 6, 2018 at 2:59
  • AAaah, that is good!!! That is what I am searching for. Commented Apr 6, 2018 at 3:01
  • And this github.com/Microsoft/TypeScript/issues/2559 Commented Apr 6, 2018 at 3:15

3 Answers 3

1

Try this:

const aaa = new DLOinterval<number, DLOnumber>(new DLOnumber(3), new DLOnumber(5), null);
Sign up to request clarification or add additional context in comments.

5 Comments

good point. In the fiddle I actually used T as types for the constructor and call new.. within the constructor, but in both cases I get ReferenceError: U is not defined
Thanks, indeed the type alias didn't work, but direct instantiation does.
The constructor I had was wrong: constructor(s: T, e: T, d: any) { this.start = new U(s); ...} didn't work, U is not defined.
BTW, do you know why const bar = new DLOnumber('hello'); this doesn't give an error?
It does for me: "Argument of type '"hello"' is not assignable to parameter of type 'number'."
0

You do not need to create a type from the DLOinterval class, you can directly use it as follows:

const ggg = new DLOinterval<number, DLOnumber>( new DLOnumber(3), new DLOnumber(5), null);

Comments

0

The constraint applied to DLOinterval<T, U extends DiscreteLinearOrder<T>> doesn't compile due to the mentioned error in the comment section because it will become like a nested infinite constraint. How did it work for you?

The only way it should work correctly is by typing the second constraint as to get past the compiler's objection to typing the template type as T.

class DLOinterval<V, U extends DiscreteLinearOrder<any>>

To make a working example for a mocha test, I extended the same interface as in the screenshot below:

enter image description here

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.