mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-07-03 16:50:15 +00:00
write: initial definition (#39328)
This commit is contained in:
committed by
Wesley Wigham
parent
73334eaf52
commit
be4cd49eef
45
types/write/index.d.ts
vendored
Normal file
45
types/write/index.d.ts
vendored
Normal 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
23
types/write/tsconfig.json
Normal 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
1
types/write/tslint.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
47
types/write/write-tests.ts
Normal file
47
types/write/write-tests.ts
Normal 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 });
|
||||
Reference in New Issue
Block a user