Skip to content

Instantly share code, notes, and snippets.

@OliverJAsh
Created November 25, 2016 15:40
Show Gist options
  • Save OliverJAsh/38819ec2697fddf087983382bdd430ca to your computer and use it in GitHub Desktop.
Save OliverJAsh/38819ec2697fddf087983382bdd430ca to your computer and use it in GitHub Desktop.
Immutable.js TypeScript issues
{
// Parameter types should not be nullable
// https://github.com/facebook/immutable-js/pull/919
const xs: List<number> = List([1])
xs.forEach(x => {
x // number | undefined, should be number
})
// Workaround:
xs.forEach((x: number) => {
x // number
})
}
{
// Operators return wrong type
const xs: List<number> = List([1])
const xsPlus1A: List<number> = xs.map(x => x + 1) // Type 'Iterable<number, number>' is not assignable to type 'List<number>'.
// Workaround:
const xsPlus1B: List<number> = xs.map(x => x + 1).toList()
}
{
// Map key/value cannot be inferred
// https://github.com/facebook/immutable-js/issues/686
const mapA: Map<string, string> = Map([[ 'foo', 'bar' ]]) // Type 'Map<{}, {}>' is not assignable to type 'Map<string, string>'.
// Workaround:
const mapB: Map<string, string> = Map<string, string>([[ 'foo', 'bar' ]])
}
{
// Get should return nullable type
const xs: List<number> = List([1])
const maybeXA = xs.get(0)
maybeXA // number, should be number | undefined
// Workaround:
const maybeXB: number | undefined = xs.get(0);
maybeXB // number | undefined
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment