Skip to content

Commit ca30b46

Browse files
jirutkajorgebay
authored andcommitted
fix: Use accurate export style in TypeScript types
The current way is inaccurate because `index.js`, the `main` entrypoint, uses CommonJS and does not export `default`. Thus it works only when the library consumer enables `esModuleInterop`. Most devs have it enabled, but this option is just a workaround for badly written type declarations. https://github.com/DefinitelyTyped/DefinitelyTyped#a-package-uses-export--but-i-prefer-to-use-default-imports-can-i-change-export--to-export-default: > For an npm package, export = is accurate if `node -p 'require("foo")'` > works to import a module, and export default is accurate if > `node -p 'require("foo").default'` works to import a module.
1 parent d79be97 commit ca30b46

File tree

2 files changed

+42
-34
lines changed

2 files changed

+42
-34
lines changed

index.d.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import TailFile from './lib/tail-file'
1+
import TailFile = require('./lib/tail-file')
22

3-
export * from './lib/tail-file'
4-
export default TailFile
3+
// NOTE: Do not rewrite it into `export default` unless tail-file's `main`
4+
// entrypoint actually exports `default`.
5+
export = TailFile

lib/tail-file.d.ts

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -66,36 +66,41 @@ interface TailFileEvents extends ReadableEvents {
6666
truncated: (payload: EventPayload) => void
6767
}
6868

69-
export declare interface TailFileOptions extends ReadableOptions {
70-
/**
71-
* How often to poll filename for changes (in milliseconds).
72-
* @default 1000
73-
*/
74-
pollFileIntervalMs?: number
75-
/**
76-
* After a polling error (ENOENT?), how long to wait before retrying (in milliseconds).
77-
* @default 200
78-
*/
79-
pollFailureRetryMs?: number
80-
/**
81-
* The number of times to retry a failed poll before exiting/erroring.
82-
* @default 10
83-
*/
84-
maxPollFailures?: number
85-
/**
86-
* Options to pass to the `fs.createReadStream` function.
87-
* This is used for reading bytes that have been added to filename between every poll.
88-
*/
89-
readStreamOpts?: ReadStreamOpts
9069

91-
/**
92-
* An integer representing the initial read position in the file, or `null`
93-
* for start tailing from EOF.
94-
* Useful for reading from 0.
95-
*
96-
* @default null
97-
*/
98-
startPos?: number | null
70+
declare namespace TailFile {
71+
// NOTE: All types in this scope are implicitly exported.
72+
73+
interface TailFileOptions extends ReadableOptions {
74+
/**
75+
* How often to poll filename for changes (in milliseconds).
76+
* @default 1000
77+
*/
78+
pollFileIntervalMs?: number
79+
/**
80+
* After a polling error (ENOENT?), how long to wait before retrying (in milliseconds).
81+
* @default 200
82+
*/
83+
pollFailureRetryMs?: number
84+
/**
85+
* The number of times to retry a failed poll before exiting/erroring.
86+
* @default 10
87+
*/
88+
maxPollFailures?: number
89+
/**
90+
* Options to pass to the `fs.createReadStream` function.
91+
* This is used for reading bytes that have been added to filename between every poll.
92+
*/
93+
readStreamOpts?: ReadStreamOpts
94+
95+
/**
96+
* An integer representing the initial read position in the file, or `null`
97+
* for start tailing from EOF.
98+
* Useful for reading from 0.
99+
*
100+
* @default null
101+
*/
102+
startPos?: number | null
103+
}
99104
}
100105

101106
declare class TailFile extends Readable {
@@ -109,7 +114,7 @@ declare class TailFile extends Readable {
109114
* @param opts Optional options.
110115
* @throws {TypeError | RangeError} if parameter validation fails.
111116
*/
112-
constructor(filename: string, opts?: TailFileOptions)
117+
constructor(filename: string, opts?: TailFile.TailFileOptions)
113118

114119
/**
115120
* Beings the polling of `filename` to watch for added/changed bytes.
@@ -155,4 +160,6 @@ declare class TailFile extends Readable {
155160
removeListener(event: string | symbol, listener: (...args: any[]) => void): this
156161
}
157162

158-
export default TailFile
163+
// NOTE: Do not rewrite it into `export default` unless tail-file's `main`
164+
// entrypoint actually exports `default`.
165+
export = TailFile

0 commit comments

Comments
 (0)