0

Sample:

@Pipe({ name: 'values',  pure: false })
export class ValuesPipe implements PipeTransform {
  transform(value: any, args: any[] = null): any {
    return Object.keys(value).map(key => value[key]);
  }
}

It appears that class inheritance is not supported in ES6. Is there an alternative that doesn't require reverse-engineering the super class?

Evan Plaice
  • 13,944
  • 6
  • 76
  • 94
  • Is compiling with typescript not an option? – Langley Jan 13 '16 at 20:24
  • @Langley This answer is marked with the `ecmascript-6` tag for a reason. If I intended to rewrite my whole app from scratch in TS, I wouldn't bother asking. Beside, NG2 is supposed to be ES6 friendly. Eventually other users will target ES6 and run into the same problem, so it's not exclusive to my implementation. – Evan Plaice Jan 13 '16 at 21:04
  • I am merely trying to get info about your scenario, tsconfig.ts has an ES6 option for the target, I wonder if that allows you to use ts without having to rewrite your whole app. – Langley Jan 13 '16 at 21:09
  • @Langley I'm using Traceur with decorators enabled. I'm not really looking to change or further complicate the transpile/build process. It works well as is. Thanks for your help anyway. – Evan Plaice Jan 13 '16 at 21:40
  • you can try and look at this answer [http://stackoverflow.com/a/34375152/3106920](http://stackoverflow.com/a/34375152/3106920) – Poul Kruijt Jan 13 '16 at 22:01

1 Answers1

1

ES6 does have class inheritance. Classes are inherited by extends.

When you use implements you are asking for type checking against an interface. If you want type checking you should be using Typescript - if you don't need type checking then you don't need implements. Using implements doesn't have an effect on the output code — it's just for type checking during compile.

For example in typescript:

class myClass{
    public myID:number
    constructor(){
    }
     talk(){
         console.log("hi there");       
    }
}

class newClass {
    public myID:number;
    talk(){
        console.log("Hi from new Class");   
    }
}

class newClassImplements implements myClass {
    public myID:number;
    talk(){
        console.log("Hi from new Class");       
    }
} 

newClass and newClassImplements both result in exactly the same javascript after compilation. The version with implements just asks the compiler to make sure it has the same interface as myClass if it doesn't you get an error at compilation.

In your sample above ValuesPipe isn't inheriting from PipeTransform it's simply implementing the interface. If you don't need the type checking you should be able to just write the function you want and forget about implementing the interface.

Mark
  • 90,562
  • 7
  • 108
  • 148