Skip to content

angularjs - fix signatures for filter service - #5948

Merged
vvakame merged 2 commits into
DefinitelyTyped:masterfrom
use-strict:angularjs-filter-service
Oct 6, 2015
Merged

vvakame merged 2 commits into
DefinitelyTyped:masterfrom
use-strict:angularjs-filter-service

Conversation

@use-strict

Copy link
Copy Markdown
Contributor

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

use-strict referenced this pull request Sep 22, 2015
angularjs - better granularity for filter service parameter types
@btesser

btesser commented Sep 23, 2015

Copy link
Copy Markdown
Contributor

@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

Comment thread angularjs/angular-tests.ts Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the convention here... single or double quotes... you seem to be using both

@use-strict

Copy link
Copy Markdown
Contributor Author

@btesser You have to create interfaces for custom filters or at least explicitly opt out of checking with $filter<any> (also see 062cd11). The generic can't really be optional since you would loose type checking. It would return any for calls to $filter which don't match the string literal overloads.

String literal overloads work fine for me with tsc 1.6.2. Can you give an example or a specific error?

@btesser

btesser commented Sep 23, 2015

Copy link
Copy Markdown
Contributor

@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')});

@use-strict

Copy link
Copy Markdown
Contributor Author

@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" any-s into the application.

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.

ts16

@vvakame

vvakame commented Sep 27, 2015

Copy link
Copy Markdown
Member

angularjs/angular.d.ts

to author(@diegovilar). could you review this PR?
👍 or 👎?

check list

  • pass the Travic-CI test?

@ChrisMBarr

Copy link
Copy Markdown
Contributor

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

@use-strict

Copy link
Copy Markdown
Contributor Author

@ChrisMBarr , added your example to the tests. Everything passes. Have to wait for PR to be merged.

Comment thread angularjs/angular.d.ts

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this also works with a string as the amount and a string as the fractionSize

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :(

@btesser

btesser commented Sep 28, 2015

Copy link
Copy Markdown
Contributor

@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.

@use-strict

Copy link
Copy Markdown
Contributor Author

@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 <T>(name: string): T; with <T>(name: number): T; and witnessing the wonderful error:

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

@use-strict

Copy link
Copy Markdown
Contributor Author

@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 :)

@vvakame
vvakame merged commit 221575e into DefinitelyTyped:master Oct 6, 2015
@vvakame

vvakame commented Oct 6, 2015

Copy link
Copy Markdown
Member

it merged. thanks mate!

@use-strict
use-strict deleted the angularjs-filter-service branch October 6, 2015 12:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants