Is PipeTraform necessary ?

26 views
Skip to first unread message

feof

unread,
Jan 14, 2017, 10:08:37 AM1/14/17
to Angular
Hi guys, I'm wondering if Pipertrasform is really essential!! If I don't use it everything works with no typescript issues. Any suggestion?
tnx!

Sander Elias

unread,
Jan 16, 2017, 12:29:48 AM1/16/17
to Angular
Hi Feof,

context? 
The pipeTransform interface is accepting almost anything. It only enforces you to have at least 2 parameters, and you must return something. And those are mandatory anyway, so can you show us what is giving you the errors?

Regards
Sander
Message has been deleted

feof

unread,
Jan 17, 2017, 4:38:15 AM1/17/17
to Angular
Hi, obviously i did not explain.
import { Component, Pipe, PipeTransform} from '@angular/core';

@Component({
  selector: 'app-root',
  template: `<div>{{test | aux:'Rossi'}}</div>`
})
export class AppComponent {
  test:string = 'test';
  constructor(){}
}

@Pipe({name: 'aux'})
export class HelloPipe implements PipeTransform{
  transform(nome: number, args: [string]): void {
  }
}

OR

import { Component, Pipe} from '@angular/core';

@Component({
  selector: 'app-root',
  template: `<div>{{test | aux:'Rossi'}}</div>`
})
export class AppComponent {
  test:string = 'test';
  constructor(){}
}

@Pipe({name: 'aux'})
export class HelloPipe{
  transform(nome: number, args: [string]): void {
  }
}

where is the difference, if i don't use pipetrasform typescript doesn't give errors
Tnx

Sander Elias

unread,
Jan 17, 2017, 6:57:25 AM1/17/17
to Angular
Hi Feof,


The problem is that you created a void function. a pipe MUST return something. Fix that, and your error is gone.

Regards
Sander

feof

unread,
Jan 17, 2017, 8:51:32 AM1/17/17
to Angular
at this point I think that the problem is my English.I have no errors, I do not understand what is pipetrasform! I used voluntarily void

import { Component, Pipe} from '@angular/core';

@Component({
  selector: 'app-root',
  template: `<div>{{test | aux:'me'}}</div>`
})
export class AppComponent {
  test:string = 'Help';
  constructor(){}
}

@Pipe({name: 'aux'})
export class HelpPipe {
  transform(str: string, ...args: any[]): string {
    console.log(str);
    return `${str} ${args[0]}`;
  }
}

This source works perfectly, if you add "PipeTrasform" is same.
I hope this time I explained.

Thierry Ciot

unread,
Jan 18, 2017, 2:21:56 PM1/18/17
to Angular
May be this will help you (extracted from https://angular.io/docs/ts/latest/guide/lifecycle-hooks.html):

Interface are optional (technically)

The interfaces are optional for JavaScript and Typescript developers from a purely technical perspective. The JavaScript language doesn't have interfaces. Angular can't see TypeScript interfaces at runtime because they disappear from the transpiled JavaScript.

Fortunately, they aren't necessary. You don't have to add the lifecycle hook interfaces to directives and components to benefit from the hooks themselves.

Angular instead inspects directive and component classes and calls the hook methods if they are defined. Angular finds and calls methods like ngOnInit(), with or without the interfaces.

Nonetheless, it's good practice to add interfaces to TypeScript directive classes in order to benefit from strong typing and editor tooling.

Reply all
Reply to author
Forward
0 new messages