Generics 'T' is not assignable to parameter of type 'never'typescript

Viewed 86

working with simple generics argument example but ended up with two transpile time error

Argument of type 'T' is not assignable to parameter of type 'never' for this.data.push(item)

'T' could be instantiated with an arbitrary type which could be unrelated to 'undefined' for return this.data.shift()

can anyone explain this error?

 class Queue<T>{
        data = [];
        push(item: T){ this.data.push(item); }
        pop(): T { return this.data.shift(); }
    }
    
    const queue = new Queue<number>();
    
    queue.push(123);
    queue.push(456);
    
    console.log(queue.pop().toPrecision(1));
    console.log(queue.pop().toPrecision(1));
0 Answers
Related