write: initial definition (#39328)

This commit is contained in:
Junxiao Shi
2019-10-22 16:57:12 -04:00
committed by Wesley Wigham
parent 73334eaf52
commit be4cd49eef
4 changed files with 116 additions and 0 deletions

45
types/write/index.d.ts vendored Normal file
View File

@@ -0,0 +1,45 @@
// Type definitions for write 2.0
// Project: https://github.com/jonschlinkert/write
// Definitions by: Junxiao Shi <https://github.com/yoursunny>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.5
/// <reference types="node" />
import * as fs from "fs";
type Data = string|Buffer|Uint8Array;
interface CommonOptions {
newline?: boolean;
overwrite?: boolean;
increment?: boolean;
}
type Options = Omit<fs.WriteFileOptions, "encoding"> &
Omit<fs.MakeDirectoryOptions, "recursive"> &
CommonOptions;
type CreateWriteStreamOptions = Extract<Parameters<typeof fs.createWriteStream>[1], Record<string, any>>;
type StreamOptions = Omit<CreateWriteStreamOptions, "encoding"> &
Omit<fs.MakeDirectoryOptions, "recursive"> &
CommonOptions;
interface Result<T extends Data> {
path: string;
data: T;
}
type Callback<T extends Data> = (err: Error|null, result?: Result<T>) => any;
declare function write<T extends Data>(filepath: string, data: T, options: Options, callback: Callback<T>): void;
declare function write<T extends Data>(filepath: string, data: T, callback: Callback<T>): void;
declare function write<T extends Data>(filepath: string, data: T, options?: Options): Promise<Result<T>>;
declare namespace write {
function sync<T extends Data>(filepath: string, data: T, options?: Options): Result<T>;
function stream(filepath: string, options?: StreamOptions): fs.WriteStream;
}
export = write;

23
types/write/tsconfig.json Normal file
View File

@@ -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"
]
}

1
types/write/tslint.json Normal file
View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }

View File

@@ -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 });