Skip to content

Better TypeScript types for inherited methods #564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vogievetsky opened this issue Jul 27, 2015 · 3 comments
Closed

Better TypeScript types for inherited methods #564

vogievetsky opened this issue Jul 27, 2015 · 3 comments
Labels

Comments

@vogievetsky
Copy link

I have started using immutable.js within a TypeScript (1.5.3) project and it is working great save for one small annoyance.

Please excuse me if this has already been asked before or if there is something obvious that I am missing.

import { List } from 'immutable';

function printNiceList(listOfStrings: List<string>): void {
  console.log(listOfStrings.pop().join(', ') + ', and ' + listOfStrings.last());
}

var countries = List(['USA', 'UK', 'Ukraine', 'Uzbekistan']);

// This works nicely:
printNiceList(countries);

// This does not pass the type check:
printNiceList(countries.rest());
// TSC errors with:
// error TS2345 Argument of type 'Iterable<number, string>' is not assignable to parameter of type 'List<string>'.
// > TS2324 Property 'set' is missing in type 'Iterable<number, string>'.

// The type needs to be explicit to get it to work:
printNiceList(<List<string>>countries.rest());

// Same issue with all inherited methods like: slice, splice, skip, concat, e.t.c

Since I work with List<Blah> a lot I end up having to insert <List<Blah>> all over the code which is a little annoying.

Would it be possible to redefine the inherited method types in the .d.ts file so that these methods' return type is List<Blah> vs Iterable<number, string> ?

@leebyron
Copy link
Collaborator

rest() returns a Seq, not a List, so you'll need to accept something more generic. I'd recommend:

printNiceList(listOfStrings: IndexedIterable<string>)

@vogievetsky
Copy link
Author

Hi @leebyron ,

Thank you for the reply.

You can not change the type signature of printNiceList because it calls pop. You would get an error of error TS2339 Property 'pop' does not exist on type 'IndexedIterable<string>'.

I am not sure what you mean when you say that rest() does not return a list as the return type of it certainly behaves like a list by having the pop method and satisfying List.isList (as one would expect).

var countries = List(['USA', 'UK', 'Ukraine', 'Uzbekistan']);
console.log(List.isList(countries.rest())); // => true

As far as I can see Immutable's logic is solid, it is only the TS type definition that is lacking as it is indication a more generic return type (and that too is only a small cast inconvenience).

@leebyron
Copy link
Collaborator

leebyron commented Mar 8, 2017

Closing this aging issue - latest in master has excellent types which will be released soon

@leebyron leebyron closed this as completed Mar 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants