Skip to content

Extending a class using interface and ES6 Proxy #28776

@JimmyPruitt

Description

@JimmyPruitt

I checked the FAQ and didn't see anything related to this problem and didn't find anything I thought looked related. I'll admit that the code snippet below doesn't come from a place of complete comprehension of Typescript and inheritance, so it's possible that a language feature exists I'm not aware of, but as it stands I can't get my code to compile.

Search Terms:

  • typescript es6 proxy interface
  • typescript interface missing property

Code

interface IArrayProxy<T> extends Array<T> {}

// @ts-ignore I'm okay with needing this because the case is somewhat exceptional
class ArrayProxy<T> implements IArrayProxy<T> {
  constructor(protected array: T[]) {
    return new Proxy(this, this);
  }

  public get(target: any, prop: string) {
    return this.hasOwnProperty(prop) ? this[prop] : this.array[prop];
  }

  // More functions below
}

const arrayProxy = new ArrayProxy([]);
console.log(arrayProxy['length']); // <--- Compiles and gives desired output
console.log(arrayProxy.length); // <-- Gives desired output but doesn't compile

Expected behavior: The entire block compiles and runs correctly

Actual behavior: The block doesn't compile because of the final line

Playground Link: link to playground

Related Issues: N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions