Skip to content

Commit dfb976f

Browse files
committedApr 13, 2023
fix: type .raw for types
·
v3.4.2v3.0.2
1 parent f9d86b6 commit dfb976f

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed
 

‎src/consola.ts‎

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,14 @@ export class Consola {
5252
...this.options.defaults,
5353
...types[type as LogType],
5454
};
55-
(this as any)[type] = this._wrapLogFn(defaults);
56-
(this as any)[type].raw = this._wrapLogFn(defaults, true);
55+
// @ts-expect-error
56+
(this as unknown as ConsolaInstance)[type as LogType] =
57+
this._wrapLogFn(defaults);
58+
// @ts-expect-error
59+
(this as unknown as ConsolaInstance)[type].raw = this._wrapLogFn(
60+
defaults,
61+
true
62+
);
5763
}
5864

5965
// Use _mockFn if is set
@@ -149,7 +155,9 @@ export class Consola {
149155
(console as any)["__" + type] = (console as any)[type]; // eslint-disable-line no-console
150156
}
151157
// Override
152-
(console as any)[type] = (this as any)[type].raw; // eslint-disable-line no-console
158+
(console as any)[type] = (this as unknown as ConsolaInstance)[
159+
type as LogType
160+
].raw; // eslint-disable-line no-console
153161
}
154162
}
155163

@@ -169,7 +177,7 @@ export class Consola {
169177
this._wrapStream(this.options.stderr, "log");
170178
}
171179

172-
_wrapStream(stream: NodeJS.WriteStream | undefined, type: string) {
180+
_wrapStream(stream: NodeJS.WriteStream | undefined, type: LogType) {
173181
if (!stream) {
174182
return;
175183
}
@@ -181,7 +189,7 @@ export class Consola {
181189

182190
// Override
183191
(stream as any).write = (data: any) => {
184-
(this as any)[type].raw(String(data).trim());
192+
(this as unknown as ConsolaInstance)[type].raw(String(data).trim());
185193
};
186194
}
187195

@@ -223,10 +231,12 @@ export class Consola {
223231
}
224232

225233
for (const type in this.options.types) {
226-
(this as any)[type] =
227-
_mockFn(type as LogType, (this as any).options.types[type]) ||
228-
(this as any)[type];
229-
(this as any)[type].raw = (this as any)[type];
234+
(this as unknown as ConsolaInstance)[type as LogType] =
235+
_mockFn(type as LogType, this.options.types[type as LogType]) ||
236+
(this as unknown as ConsolaInstance)[type as LogType];
237+
(this as unknown as ConsolaInstance)[type as LogType].raw = (
238+
this as unknown as ConsolaInstance
239+
)[type as LogType];
230240
}
231241
}
232242

@@ -365,8 +375,11 @@ function _normalizeLogLevel(
365375
return defaultLevel;
366376
}
367377

368-
export type ConsolaInstance = Consola &
369-
Record<LogType, (message: InputLogObject | any, ...args: any[]) => void>;
378+
export interface LogFn {
379+
(message: InputLogObject | any, ...args: any[]): void;
380+
raw: (...args: any[]) => void;
381+
}
382+
export type ConsolaInstance = Consola & Record<LogType, LogFn>;
370383

371384
// Legacy support
372385
// @ts-expect-error

0 commit comments

Comments
 (0)
Please sign in to comment.