Repository navigation
angularjs - fix signatures for filter service - #5948
Conversation
angularjs - better granularity for filter service parameter types
|
@use-strict @baltie @albv. In testing this I've found that it breaks any filter that isn't explicitly defining the generic. Any chance you can submit a pr that makes the use of generics optional. Also from what I've seen the string overloads are not working correctly with typescript 1.6.2 #5814 |
There was a problem hiding this comment.
what's the convention here... single or double quotes... you seem to be using both
|
@btesser You have to create interfaces for custom filters or at least explicitly opt out of checking with String literal overloads work fine for me with tsc 1.6.2. Can you give an example or a specific error? |
|
@use-strict I'd suggest we change the definition to something like: interface IFilterService {
(name: 'filter'): IFilterFilter;
(name: 'currency'): IFilterCurrency;
(name: 'number'): IFilterNumber;
(name: 'date'): IFilterDate;
(name: 'json'): IFilterJson;
(name: 'lowercase'): IFilterLowercase;
(name: 'uppercase'): IFilterUppercase;
(name: 'limitTo'): IFilterLimitTo;
(name: 'orderBy'): IFilterOrderBy;
/**
* Usage:
* $filter(name);
*
* @param name Name of the filter function to retrieve
*/
<T>(name: string): T;
(name: string): Function;
}My assumption, which may be incorrect, is that if a generic is provided, typescript will use that implementation of the interface. If one isn't provided, it will use the any approach. Regarding overloads, the way typescript will actually handle it, the following will fail to cause an error, even though one would expect it to. (the function describes the number filter) $filter('date')((amount: number, symbol?: string, fractionSize?: number) => { console.log('symbol')}); |
|
@btesser, using both declarations serves no purpose, because the less restrictive one will make the other useless. The point here is to enforce a known return type, ensuring type safety and no "leaking" So, we can only use one of the two. I'm investigating now the overload behavior. I understand the problem now, but I can't seem to reproduce it. In Visual Studio Code I see the behavior I'm expecting, including an error for your snippet (tested with 1.5.3 and 1.6.2). Command-line compiler yields the same result. |
|
angularjs/angular.d.ts to author(@diegovilar). could you review this PR? check list
|
|
This still does not allow date formatting. I should be allowed to have the following code: var angularDateFilter = $filter("date");
var myDate = new Date();
angularDateFilter(myDate , "EEE, MMM d");The above code previously compiled to JS just fine and worked, but now I get this error on build Argument of type 'Date' is not assignable to parameter of type '{}[]'. However, if I change my code to make the 1st param be an array, it works. angularDateFilter([myDate] , "EEE, MMM d");Original discussion about this on this commit: 062cd11 |
|
@ChrisMBarr , added your example to the tests. Everything passes. Have to wait for PR to be merged. |
There was a problem hiding this comment.
this also works with a string as the amount and a string as the fractionSize
There was a problem hiding this comment.
Are you sure about this? The documentation says they should be numbers. Judging from the source code (https://github.com/angular/angular.js/blob/v1.4.6/src/ng/filter/filters.js#L137), it might be possible, though it doesn't seem right calling Math.abs on a string :(
|
@use-strict it seems you are right about the string arguments. What are they speaking about in the typescript docs when they say this then? From the typescript docs regarding how overload call signatures work: Functions with specialized overload signatures (those that use string literals in their overloads) do not use their specialized signatures when checking for compatibility. |
|
@btesser, there are two types of overloads: specialized and non-specialized. As stated in the documetation, a specialized overload is an overload that uses string literals. So what I understand from their statement is this: "[...] do not use their specialized signatures when checking for compatibility" is the same as "only non-specialized overload signatures and the function implementation are used for determining compatibility". On top of this, specialized signatures must be assignable to at least one non-specialized signature. You can test this in our case by replacing Some more "light reading" here: https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.9.2.4 and here: https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#4.15.1 |
|
@vvakame , is there a way to speed up the process here? This is really ugly for angular users an tbh it can't be worse than not working :) |
|
it merged. thanks mate! |

Added overloads for each filter type and a generic filter getter. Fixes definition problems introduced by #5814