angular icon indicating copy to clipboard operation
angular copied to clipboard

Pipes with generic type parameters don't typecheck

Open ArielGueta opened this issue 3 years ago • 1 comments

Which @angular/* package(s) are the source of the bug?

language-service

Is this a regression?

No

Description

Lets say we have a pipe that returns a number:

@Pipe({
  name: 'test'
})
export class TestPipe<T> implements PipeTransform {

  transform(value: T): number {
    return 1;
  }
}

When we use it on input that expects a string it works fine without a type error:

@Component({
  selector: 'app-foo'
})
export class FooComponent {
  @Input() text: string;
}

<app-foo [text]="'bar' | test"></app-foo>

This should show a type error because the input expects a string and the pipe returns a number.

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in (run ng version)

No response

Anything else?

I'm using strictTemplates: true

ArielGueta avatar Jul 27 '22 09:07 ArielGueta

Pipes with generic type parameters are not currently supported. In your case the T type parameter can be dropped in favor of using unknown where it's used.

JoostK avatar Jul 27 '22 10:07 JoostK

Just got caught by this. Any technical reason or just a nice to have that no one has every tried to implement? Curious because it perhaps its reasonable to take on as a first time contributor?

jarpoole avatar Aug 13 '23 12:08 jarpoole

It would require a different approach to how pipes are declared in the type-checker, specifically using a type constructor. A lot more details are in https://github.com/angular/angular/pull/40870#issuecomment-780882880. There's also this comment.

JoostK avatar Aug 15 '23 19:08 JoostK