TypeScript Version: 2.2.1
Code
With the code
functionfilterMap<T,U>(data: Array<T>,fn: (element: T)=>U): Array<U>{letnewArray: Array<U>=[]data.forEach(element=>{letnewElement=fn(element)if(newElement!=null){newArray.push(newElement)}})returnnewArray}letfruits=['apple','banana','pear']letfilteredAndMapped=filterMap(fruits,e=>e.startsWith('a') ? null : e.toUpperCase())filteredAndMapped now have the type Array<string | null> but in reality only contain elements of type string.
It would be nice if it was possible to do a non-null assertion or similar for types, allowing me to write something similar to
function filterMap<T, U>(data: Array<T>, fn: (element: T) => U): Array<U!> {
let newArray: Array<U!> = []
// ...
so that filteredAndMapped and newArray have the type Array<string>.
TypeScript Version: 2.2.1
Code
With the code
filteredAndMappednow have the typeArray<string | null>but in reality only contain elements of type string.It would be nice if it was possible to do a non-null assertion or similar for types, allowing me to write something similar to
so that
filteredAndMappedandnewArrayhave the typeArray<string>.