diff --git a/types/write/index.d.ts b/types/write/index.d.ts new file mode 100644 index 0000000000..5be053e0a8 --- /dev/null +++ b/types/write/index.d.ts @@ -0,0 +1,45 @@ +// Type definitions for write 2.0 +// Project: https://github.com/jonschlinkert/write +// Definitions by: Junxiao Shi +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 3.5 + +/// +import * as fs from "fs"; + +type Data = string|Buffer|Uint8Array; + +interface CommonOptions { + newline?: boolean; + overwrite?: boolean; + increment?: boolean; +} + +type Options = Omit & + Omit & + CommonOptions; + +type CreateWriteStreamOptions = Extract[1], Record>; + +type StreamOptions = Omit & + Omit & + CommonOptions; + +interface Result { + path: string; + data: T; +} + +type Callback = (err: Error|null, result?: Result) => any; + +declare function write(filepath: string, data: T, options: Options, callback: Callback): void; +declare function write(filepath: string, data: T, callback: Callback): void; +declare function write(filepath: string, data: T, options?: Options): Promise>; + +declare namespace write { + function sync(filepath: string, data: T, options?: Options): Result; + + function stream(filepath: string, options?: StreamOptions): fs.WriteStream; +} + +export = write; diff --git a/types/write/tsconfig.json b/types/write/tsconfig.json new file mode 100644 index 0000000000..a464cb51bd --- /dev/null +++ b/types/write/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "write-tests.ts" + ] +} diff --git a/types/write/tslint.json b/types/write/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/write/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/write/write-tests.ts b/types/write/write-tests.ts new file mode 100644 index 0000000000..4fae5e908a --- /dev/null +++ b/types/write/write-tests.ts @@ -0,0 +1,47 @@ +import write = require("write"); + +const text = "file " + "content"; +const buf = Buffer.alloc(2); +const arr = Uint8Array.of(0x19, 0x87, 0x02, 0x12); + +write("1.txt", text).then(({ path, data }) => { + // $ExpectType string + path; + // $ExpectType string + data; +}); + +write("1.txt", buf, { mode: 0o777 }).then(({ data }) => { + // $ExpectType Buffer + data; +}); + +write("1.txt", arr, (err, result) => { + if (err) { + // $ExpectType Error + err; + } + if (result) { + const { path, data } = result; + // $ExpectType string + path; + // $ExpectType Uint8Array + data; + } +}); + +// $ExpectType void +write("1.txt", text, { newline: true, overwrite: true, increment: true }, () => "ok"); + +let { path, data } = write.sync("1.txt", arr); +// $ExpectType string +path; +// $ExpectType Uint8Array +data; + +({ path, data } = write.sync("1.txt", arr, { mode: 0o777, newline: true })); + +// $ExpectType WriteStream +write.stream("1.txt"); + +write.stream("1.txt", { highWaterMark: 8, overwrite: true });