funkia / list

🐆 An immutable list with unmatched performance and a comprehensive functional API.
MIT License
1.65k stars 51 forks source link

Filter handles user-defined type guards #43

Closed paldepind closed 6 years ago

paldepind commented 6 years ago

This PR improves the types of filter such that it better handles user-defined type guards. This idea was taken from @nordfjord's work here: https://github.com/paldepind/flyd/pull/173

Example.

function isString(a: any): a is string {
  return typeof a === "string";
}
// A list containing elements of several different types
const l = L.list<number | string>(0, "one", 2, "three", 4, "five");
// After filtering with the type guard the returned list will now have a more precise type
const l2 = L.filter(isString, l);
// TypeScript now knows that l2 contains only strings :)
const l3 = L.map(s => s.padStart(6), l2);
codecov[bot] commented 6 years ago

Codecov Report

Merging #43 into master will increase coverage by <.01%. The diff coverage is 100%.

Impacted Files Coverage Δ
src/curried.ts 100% <ø> (ø) :arrow_up:
src/index.ts 97.91% <100%> (ø) :arrow_up: