diff --git a/types/ncp/index.d.ts b/types/ncp/index.d.ts index dddb7da483..738281fcb0 100644 --- a/types/ncp/index.d.ts +++ b/types/ncp/index.d.ts @@ -1,20 +1,71 @@ -// Type definitions for ncp v2.0.0 +// Type definitions for ncp 2.0 // Project: https://github.com/AvianFlu/ncp // Definitions by: Bart van der Schoor // Benoit Lemaire +// ExE Boss // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// +import * as fs from 'fs'; -export function ncp(source: string, destination: string, callback: (err: Error) => void): void; -export function ncp(source: string, destination: string, options: Options, callback: (err: Error) => void): void; +declare namespace ncp { + interface File { + name: string; + mode: number; -interface Options { - filter?: RegExp | ((filename: string) => boolean); - transform?: (read: NodeJS.ReadableStream, write: NodeJS.WritableStream) => void; - clobber?: boolean; - dereference?: boolean; - stopOnErr?: boolean; - errs?: NodeJS.WritableStream; - limit?: number; + /** Accessed time */ + atime: Date; + + /** Modified time */ + mtime: Date; + } + + interface Options { + filter?: RegExp | ((filename: string) => boolean); + transform?: (read: NodeJS.ReadableStream, write: NodeJS.WritableStream, file: File) => void; + clobber?: boolean; + dereference?: boolean; + stopOnErr?: boolean; + errs?: fs.PathLike; + limit?: number; + } } + +declare const ncp: { + (source: string, destination: string, callback: (err: Error[] | null) => void): void; + ( + source: string, + destination: string, + options: ncp.Options & { stopOnErr: true }, + callback: (err: Error | null) => void, + ): void; + ( + source: string, + destination: string, + options: ncp.Options & { errs?: undefined }, + callback: (err: Error[] | null) => void, + ): void; + ( + source: string, + destination: string, + options: ncp.Options & { errs: fs.PathLike }, + callback: (err: fs.WriteStream | null) => void, + ): void; + ( + source: string, + destination: string, + options: ncp.Options, + callback: (err: Error | Error[] | fs.WriteStream | null) => void, + ): void; + + ncp: typeof ncp; + + /** + * **NOTE:** This function provides design-time support for util.promisify. + * + * It does not exist at runtime. + */ + __promisify__(source: string, destination: string, options?: ncp.Options): Promise; +}; + +export = ncp; diff --git a/types/ncp/ncp-tests.ts b/types/ncp/ncp-tests.ts index 2f30216dc2..c0ce1c5a73 100644 --- a/types/ncp/ncp-tests.ts +++ b/types/ncp/ncp-tests.ts @@ -1,39 +1,80 @@ import ncp = require('ncp'); -import stream = require('stream'); +import { ncp as ncpCB } from 'ncp'; +import * as util from 'util'; -var opts: ncp.Options; -opts = {}; -opts = { - filter: /abc/ -}; -opts = { - filter: (filename: string) => true -}; -opts = { - transform: (read: NodeJS.ReadableStream, write: NodeJS.WritableStream) => { +/** + * The `expectType` function from https://www.npmjs.com/package/tsd, + * except instead of returning `void`, it returns `T`. + */ +declare function expectType(t: T): T; - } -}; +let opts: ncp.Options = {}; +const errs = Buffer.alloc(4096); +const opts$StreamErr = (opts = { errs }); +opts = { filter: /abc/ }; +opts = { filter: (filename: string) => true }; opts = { - clobber: false -}; -opts = { - dereference: false -}; -opts = { - stopOnErr: false -}; -opts = { - errs: new stream.Writable() -}; -opts = { - limit: 512 + transform: (read: NodeJS.ReadableStream, write: NodeJS.WritableStream, file: ncp.File) => { + file; // $ExpectType File + file.name; // $ExpectType string + file.mode; // $ExpectType number + file.atime; // $ExpectType Date + file.mtime; // $ExpectType Date + }, }; +opts = { clobber: false }; +opts = { dereference: false }; +opts = { stopOnErr: false }; +opts = { limit: 512 }; -ncp.ncp('foo', 'bar', (err: Error) => { +// $ExpectType Options +opts; +ncp('foo', 'bar', err => { + err; // $ExpectType Error[] | null }); -ncp.ncp('foo', 'bar', opts, (err: Error) => { +ncp( + 'foo', + 'bar', + { + stopOnErr: true, + }, + err => { + err; // $ExpectType Error | null + }, +); +ncp( + 'foo', + 'bar', + { + errs, + stopOnErr: true, + }, + err => { + err; // $ExpectType Error | null + }, +); + +ncp('foo', 'bar', opts$StreamErr, err => { + err; // $ExpectType WriteStream | null }); + +ncp.ncp('foo', 'bar', err => { + err; // $ExpectType Error[] | null +}); + +ncp.ncp('foo', 'bar', opts, err => { + err; // $ExpectType Error | Error[] | WriteStream | null +}); + +// $ExpectType (source: string, destination: string, options?: Options | undefined) => Promise +expectType<(source: string, destination: string, options?: ncp.Options) => Promise>(util.promisify(ncp)); + +// $ExpectType (source: string, destination: string, options?: Options | undefined) => Promise +expectType<(source: string, destination: string, options?: ncp.Options) => Promise>(util.promisify(ncp.ncp)); + +// Both import forms are identical +expectType(ncpCB); +expectType(ncp.ncp); diff --git a/types/ncp/tsconfig.json b/types/ncp/tsconfig.json index ad2247a6c9..eecaff2294 100644 --- a/types/ncp/tsconfig.json +++ b/types/ncp/tsconfig.json @@ -1,23 +1,19 @@ { - "compilerOptions": { - "module": "commonjs", - "lib": [ - "es6" - ], - "noImplicitAny": true, - "noImplicitThis": true, - "strictNullChecks": false, - "strictFunctionTypes": true, - "baseUrl": "../", - "typeRoots": [ - "../" - ], - "types": [], - "noEmit": true, - "forceConsistentCasingInFileNames": true - }, - "files": [ - "index.d.ts", - "ncp-tests.ts" - ] -} \ No newline at end of file + "compilerOptions": { + "module": "commonjs", + "lib": ["es6"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": ["../"], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "ncp-tests.ts" + ] +} diff --git a/types/ncp/tslint.json b/types/ncp/tslint.json index 3d59f55fda..3db14f85ea 100644 --- a/types/ncp/tslint.json +++ b/types/ncp/tslint.json @@ -1,80 +1 @@ -{ - "extends": "dtslint/dt.json", - "rules": { - "adjacent-overload-signatures": false, - "array-type": false, - "arrow-return-shorthand": false, - "ban-types": false, - "callable-types": false, - "comment-format": false, - "dt-header": false, - "npm-naming": false, - "eofline": false, - "export-just-namespace": false, - "import-spacing": false, - "interface-name": false, - "interface-over-type-literal": false, - "jsdoc-format": false, - "max-line-length": false, - "member-access": false, - "new-parens": false, - "no-any-union": false, - "no-boolean-literal-compare": false, - "no-conditional-assignment": false, - "no-consecutive-blank-lines": false, - "no-construct": false, - "no-declare-current-package": false, - "no-duplicate-imports": false, - "no-duplicate-variable": false, - "no-empty-interface": false, - "no-for-in-array": false, - "no-inferrable-types": false, - "no-internal-module": false, - "no-irregular-whitespace": false, - "no-mergeable-namespace": false, - "no-misused-new": false, - "no-namespace": false, - "no-object-literal-type-assertion": false, - "no-padding": false, - "no-redundant-jsdoc": false, - "no-redundant-jsdoc-2": false, - "no-redundant-undefined": false, - "no-reference-import": false, - "no-relative-import-in-test": false, - "no-self-import": false, - "no-single-declare-module": false, - "no-string-throw": false, - "no-unnecessary-callback-wrapper": false, - "no-unnecessary-class": false, - "no-unnecessary-generics": false, - "no-unnecessary-qualifier": false, - "no-unnecessary-type-assertion": false, - "no-useless-files": false, - "no-var-keyword": false, - "no-var-requires": false, - "no-void-expression": false, - "no-trailing-whitespace": false, - "object-literal-key-quotes": false, - "object-literal-shorthand": false, - "one-line": false, - "one-variable-per-declaration": false, - "only-arrow-functions": false, - "prefer-conditional-expression": false, - "prefer-const": false, - "prefer-declare-function": false, - "prefer-for-of": false, - "prefer-method-signature": false, - "prefer-template": false, - "radix": false, - "semicolon": false, - "space-before-function-paren": false, - "space-within-parens": false, - "strict-export-declare-modifiers": false, - "trim-file": false, - "triple-equals": false, - "typedef-whitespace": false, - "unified-signatures": false, - "void-return": false, - "whitespace": false - } -} +{ "extends": "dtslint/dt.json" }