#Array filter method predicting wrong type
18 messages · Page 1 of 1 (latest)
it's because of how Array.filter works
you have an array of type string | undefined
and filter only removes some elements of the array
it doesn't change the type of the returned element
meaning the result will also be an array of string | undefined
just with less results
I see...
however, you can change the return type of the function used in the filter to fix that
Anyway I can case filtered to string[] by inferring array's type, without stating string[]?
const array: (string | undefined)[] = [];
const filtered = array.filter((v): v is string => !!v);
you can tell ts that the function has a specific meaning ^
!ts filtered
const filtered: string[] /* 2:7 */```
I know this, I meant to somehow use TS statements to infer the non-undefined type from array. In reality I have a type that is part of some namespace that I don't have access to because it's not exported, so I'd like to have the filtered array typed correctly without have access to the explicit type
ts doesn't do inferment across functions
you could extract that type by doing some type shenanigans ⬇️
type X = string;
declare const arr: (X | undefined)[];
type NewX = typeof arr extends Array<infer E> ? Exclude<E, undefined> : never
// ^? - type NewX = string
const res = arr.filter((v): v is NewX => !!v);
// ^? - const res: string[]