Skip to content

Commit 43f208e

Browse files
committedJan 12, 2021
refactor(TS): Add type definitions for global variables
1 parent 8fa458a commit 43f208e

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed
 

‎index.d.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { LogHandler } from './index'
2+
3+
declare global {
4+
namespace NodeJS {
5+
interface Global {
6+
_logga?: LogHandler[]
7+
}
8+
}
9+
10+
interface Window {
11+
_logga?: LogHandler[]
12+
}
13+
}

‎index.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -48,26 +48,23 @@ export interface LogHandler {
4848

4949
// Global `logga` instance
5050

51-
const root =
51+
const root: NodeJS.Global | Window =
5252
typeof window !== 'undefined'
5353
? window
5454
: typeof global !== 'undefined'
5555
? global
5656
: // Ignore in coverage because is expected to be unreachable
5757
// istanbul ignore next
58-
{}
58+
({} as typeof global)
5959

6060
function Logga(): LogHandler[] {
6161
const name = '_logga'
6262
if (name in root) {
63-
// istanbul ignore next
64-
// @ts-ignore
65-
return root[name] as LogHandler[]
63+
return root[name] ?? []
6664
}
67-
// @ts-ignore
65+
6866
root[name] = []
69-
// @ts-ignore
70-
return root[name] as LogHandler[]
67+
return root[name] ?? []
7168
}
7269

7370
const logga: LogHandler[] = Logga()
@@ -334,7 +331,6 @@ if (handlers().length === 0) addHandler(defaultHandler)
334331
*
335332
* @param tag The unique application or package name
336333
*/
337-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
338334
export function getLogger(tag: string): Logger {
339335
return {
340336
error(message: string | LogEvent) {

‎tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
"moduleResolution": "node",
66
"outDir": "./dist/lib"
77
},
8-
"files": ["index.ts"]
8+
"files": ["index.ts", "index.d.ts"]
99
}

0 commit comments

Comments
 (0)
Please sign in to comment.