diff --git a/src/core/FS.ts b/src/core/FS.ts index 5120e7a5..5e62a9bd 100644 --- a/src/core/FS.ts +++ b/src/core/FS.ts @@ -77,7 +77,8 @@ function normalizePath(p: string): string { * @hidden */ function normalizeOptions(options: any, defEnc: string | null, defFlag: string, defMode: number | null): {encoding: string; flag: string; mode: number} { - switch (typeof options) { + // typeof null === 'object' so special-case handing is needed. + switch (options === null ? 'null' : typeof options) { case 'object': return { encoding: typeof options['encoding'] !== 'undefined' ? options['encoding'] : defEnc, @@ -90,12 +91,16 @@ function normalizeOptions(options: any, defEnc: string | null, defFlag: string, flag: defFlag, mode: defMode! }; - default: + case 'null': + case 'undefined': + case 'function': return { encoding: defEnc!, flag: defFlag, mode: defMode! }; + default: + throw new TypeError(`"options" must be a string or an object, got ${typeof options} instead.`); } }