fix(#709): strict Log4js typings

* Strictly type the exported Log4js interface.
* Test the typings for strictness using the TypesScript compiler.
This commit is contained in:
Peter Safranek 2018-05-12 14:03:32 -07:00
parent 6598898c4c
commit d39b5eb36b
5 changed files with 31 additions and 12 deletions

6
package-lock.json generated
View File

@ -8570,6 +8570,12 @@
"integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=",
"dev": true
},
"typescript": {
"version": "2.8.3",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-2.8.3.tgz",
"integrity": "sha512-K7g15Bb6Ra4lKf7Iq2l/I5/En+hLIHmxWZGq3D4DIRNFxMNV6j2SHSvDOqs2tGd4UvD/fJvrwopzQXjLrT7Itw==",
"dev": true
},
"uglify-js": {
"version": "2.8.29",
"resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-2.8.29.tgz",

View File

@ -29,11 +29,12 @@
},
"scripts": {
"clean": "find test -type f ! -name '*.json' ! -name '*.js' ! -name '.eslintrc' -delete && rm *.log",
"prepush": "npm test",
"prepush": "npm test && npm run typings",
"commitmsg": "validate-commit-msg",
"posttest": "npm run clean",
"pretest": "eslint 'lib/**/*.js' 'test/**/*.js'",
"test": "tap 'test/tap/**/*.js'",
"typings": "tsc -p types/tsconfig.json",
"coverage": "tap 'test/tap/**/*.js' --cov",
"codecov": "tap 'test/tap/**/*.js' --cov --coverage-report=lcov && codecov"
},
@ -59,6 +60,7 @@
"nyc": "^11.7.3",
"sandboxed-module": "^2.0.3",
"tap": "^11.1.5",
"typescript": "^2.8.3",
"validate-commit-msg": "^2.14.0"
},
"optionalDependencies": {

23
types/log4js.d.ts vendored
View File

@ -1,12 +1,13 @@
// Type definitions for log4js
export interface Log4js {
getLogger,
configure,
addLayout,
connectLogger,
levels,
shutdown
getLogger(category?: string): Logger;
configure(filename: string): Log4js;
configure(config: Configuration): Log4js;
addLayout(name: string, config: (a: any) => (logEvent: LoggingEvent) => string): void;
connectLogger(logger: Logger, options: { format?: string; level?: string; nolog?: any; }): any; // express.Handler;
levels(): Levels;
shutdown(cb?: (error: Error) => void): void | null;
}
export function getLogger(category?: string): Logger;
@ -113,8 +114,8 @@ export interface FileAppender {
layout?: Layout;
numBackups?: number;
compress?: boolean; // compress the backups
// keep the file extension when rotating logs
keepFileExt?: boolean;
// keep the file extension when rotating logs
keepFileExt?: boolean;
encoding?: string;
mode?: number;
flags?: string;
@ -161,8 +162,8 @@ export interface DateFileAppender {
compress?: boolean;
// include the pattern in the name of the current log file as well as the backups.(default false)
alwaysIncludePattern?: boolean;
// keep the file extension when rotating logs
keepFileExt?: boolean;
// keep the file extension when rotating logs
keepFileExt?: boolean;
// if this value is greater than zero, then files older than that many days will be deleted during log rolling.(default 0)
daysToKeep?: number;
}
@ -432,7 +433,7 @@ export interface Logger {
isLevelEnabled(level?: string): boolean;
isTraceEnabled(): boolean;
isTraceEnabled(): boolean;
isDebugEnabled(): boolean;
isInfoEnabled(): boolean;
isWarnEnabled(): boolean;

View File

@ -52,6 +52,7 @@ log4js.configure({
});
const logger4 = log4js.getLogger('thing');
logger4.log('logging a thing');
const logger5 = log4js.getLogger('json-test');
logger5.info('this is just a test');

9
types/tsconfig.json Normal file
View File

@ -0,0 +1,9 @@
{
"compileOnSave": false,
"compilerOptions": {
"strict": true,
"noUnusedParameters": true,
"noUnusedLocals": true,
"noEmit": true
}
}