[@types/node] nodejs stream.Readable new methods (map, filter etc) don't have typescript types yet #60753
Replies: 3 comments 9 replies
-
Thanks for the discussion about "node", some useful links for everyone: Pinging the DT module owners: @microsoft, @DefinitelyTyped, @jkomyno, @alvis, @r3nya, @btoueg, @smac89, @Touffy, @DeividasBakanas, @eyqs, @Hannes-Magnusson-CK, @hoo29, @kjin, @ajafff, @islishude, @mwiktorczyk, @mohsen1, @n-e, @galkin, @parambirs, @eps1lon, @SimonSchick, @ThomasdenH, @WilcoBakker, @wwwy3y3, @samuela, @kuehlein, @bhongy, @chyzwar, @trivikr, @yoursunny, @qwelias, @ExE-Boss, @peterblazejewicz, @addaleax, @victorperin, @ZYSzys, @nodejs, @LinusU, @wafuwafu13, @mcollina. |
Beta Was this translation helpful? Give feedback.
-
For anyone who wants a quick fix, add a file to your project named import { AbortSignal } from 'abort-controller'
interface SignalOptions{
signal?: AbortSignal
}
interface ConcurrencyOptions{
concurrency?: number
}
declare module 'stream' {
interface Readable {
map<T>(fn: (data: any, options?: SignalOptions) => T|Promise<T>, options?: SignalOptions & ConcurrencyOptions): Readable
filter(fn: (data: any, options?: SignalOptions) => boolean|Promise<boolean>, options?: SignalOptions & ConcurrencyOptions): Readable
forEach(fn: (data: any, options?: SignalOptions) => void|Promise<void>, options?: SignalOptions & ConcurrencyOptions): Promise<void>
toArray(options?: SignalOptions): Promise<any[]>
some(fn: (data: any, options?: SignalOptions) => boolean|Promise<boolean>, options?: SignalOptions & ConcurrencyOptions): Promise<boolean>
find(fn: (data: any, options?: SignalOptions) => boolean|Promise<boolean>, options?: SignalOptions & ConcurrencyOptions): Promise<any|undefined>
every(fn: (data: any, options?: SignalOptions) => boolean|Promise<boolean>, options?: SignalOptions & ConcurrencyOptions): Promise<boolean>
flatMap<T>(fn: (data: any, options?: SignalOptions) => T|Promise<T>, options?: SignalOptions & ConcurrencyOptions): Readable
drop(limit: number, options?: SignalOptions): Readable
take(limit: number, options?: SignalOptions): Readable
asIndexedPairs(options?: SignalOptions): Readable
reduce<T>(fn: (previous:any, data:any, options?: SignalOptions)=>T|Promise<T>, initial?: T, options?: SignalOptions): Promise<T>
}
} In writing these, I discovered that Readable is not typed generically. Readable.from([1,2,3,4]) // type Readable has the same type as Readable.from(['a','b','c']) // type Readable Perhaps this is a seperate issue, but I really think it would be better for these to be typed generically such as Readable.from([1,2,3]).toArray() // should have type Promise<number[]>, but atm its just Promise<any[]> |
Beta Was this translation helpful? Give feedback.
-
same error here |
Beta Was this translation helpful? Give feedback.
-
In December 2021-January 2022 there were new methods added to the Nodejs stream.Readable class. (you can see the PR's here: https://github.com/nodejs/node/pulls?q=is%3Apr+is%3Aclosed+author%3Abenjamingr+label%3Astream+%22stream%3A+add+%22). But these do not yet have types in
@types/node
so they can't be used in typescript projects.The official documentation can be found here: https://nodejs.org/api/stream.html#readablemapfn-options
The methods are: ( version added indicated next to each one, from docs)
map
(v16.14.0 and v17.4.0)filter
(v16.14.0 and v17.4.0)forEach
(v17.5.0)toArray
(v17.5.0)some
(v17.5.0)find
(v17.5.0)every
(v17.5.0)flatMap
(v17.5.0)drop
(v17.5.0)take
(v17.5.0)asIndexedPairs
(v17.5.0)reduce
(v17.5.0)Example usage (from docs):
Current typescript:
And the same error for all the other methods.
I propose that these new methods are added to the types in https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/stream.d.ts#L50
Beta Was this translation helpful? Give feedback.
All reactions