Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeScript type definitions #801

Merged
merged 5 commits into from
Feb 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ script:
- npm run test
after_script:
- npm run coveralls
- npm run dtslint
sudo: false
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"file",
"fsevents"
],
"types": "./types/index.d.ts",
"homepage": "https://github.com/paulmillr/chokidar",
"author": "Paul Miller (https://paulmillr.com), Elan Shanker",
"repository": {
Expand All @@ -23,11 +24,13 @@
"license": "MIT",
"scripts": {
"test": "nyc mocha --exit",
"coveralls": "nyc report --reporter=text-lcov | coveralls"
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"dtslint": "dtslint types"
},
"files": [
"index.js",
"lib/"
"lib/",
"types/index.d.ts"
],
"dependencies": {
"anymatch": "^2.0.0",
Expand All @@ -46,8 +49,10 @@
"fsevents": "^1.2.7"
},
"devDependencies": {
"@types/node": "^11.9.4",
"chai": "^3.2.0",
"coveralls": "^3.0.1",
"dtslint": "0.4.1",
"graceful-fs": "4.1.4",
"mocha": "^5.2.0",
"nyc": "^11.8.0",
Expand Down
168 changes: 168 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// TypeScript Version: 3.0
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defining the specific version here works here. But I wanted to dump everything below that I looked into because someone more knowledgeable about dtslint might know more?


From what I understand, by default dtslint will check all minor versions starting from 2.0 up to next. Currently, that is 2.0, 2.1, ..., 3.3, next (will be 3.4.).

@types/parsimmon is a depedency of dtslint: (seems like types generally are devDependencies)

yarn why @types/parsimmon
=> Found "@types/parsimmon@1.10.0"
info Reasons this module exists
   - "dtslint#definitelytyped-header-parser" depends on it
   - Hoisted from "dtslint#definitelytyped-header-parser#@types#parsimmon"

And running dtslint without the version defined here was erroring:

Output of dtslint types
Installing to node_modules/dtslint/typescript-installs/2.0...

Installed!
Installing to node_modules/dtslint/typescript-installs/2.1...

Installed!
Installing to node_modules/dtslint/typescript-installs/2.2...

Installed!
Installing to node_modules/dtslint/typescript-installs/2.3...

Installed!
Installing to node_modules/dtslint/typescript-installs/2.4...

Installed!
Installing to node_modules/dtslint/typescript-installs/2.5...

Installed!
Installing to node_modules/dtslint/typescript-installs/2.6...

Installed!
Installing to node_modules/dtslint/typescript-installs/2.7...

Installed!
Installing to node_modules/dtslint/typescript-installs/2.8...

Installed!
Installing to node_modules/dtslint/typescript-installs/2.9...

Installed!
Installing to node_modules/dtslint/typescript-installs/3.0...

Installed!
Installing to node_modules/dtslint/typescript-installs/3.1...

Installed!
Installing to node_modules/dtslint/typescript-installs/3.2...

Installed!
Installing to node_modules/dtslint/typescript-installs/3.3...

Installed!
Installing to node_modules/dtslint/typescript-installs/next...

Installed!
Error: Errors in typescript@2.0 for external dependencies:
../node_modules/@types/parsimmon/index.d.ts(45,75): error TS2305: Module 'Parsimmon' has no exported member 'Reply'.
../node_modules/@types/parsimmon/index.d.ts(45,96): error TS2305: Module 'Parsimmon' has no exported member 'Parser'.
../node_modules/@types/parsimmon/index.d.ts(91,3): error TS2464: A computed property name must be of type 'string', 'number', 'symbol', or 'any'.
../node_modules/@types/parsimmon/index.d.ts(91,3): error TS7008: Member '[P in keyof' implicitly has an 'any' type.
../node_modules/@types/parsimmon/index.d.ts(91,4): error TS2304: Cannot find name 'P'.
../node_modules/@types/parsimmon/index.d.ts(91,9): error TS2304: Cannot find name 'keyof'.
../node_modules/@types/parsimmon/index.d.ts(91,15): error TS1005: ']' expected.
../node_modules/@types/parsimmon/index.d.ts(91,15): error TS2304: Cannot find name 'TLanguageSpec'.
../node_modules/@types/parsimmon/index.d.ts(91,28): error TS1005: ';' expected.
../node_modules/@types/parsimmon/index.d.ts(91,29): error TS1128: Declaration or statement expected.
../node_modules/@types/parsimmon/index.d.ts(91,49): error TS2304: Cannot find name 'TLanguageSpec'.
../node_modules/@types/parsimmon/index.d.ts(91,75): error TS2304: Cannot find name 'TLanguageSpec'.
../node_modules/@types/parsimmon/index.d.ts(91,89): error TS2304: Cannot find name 'P'.
../node_modules/@types/parsimmon/index.d.ts(91,92): error TS1109: Expression expected.
../node_modules/@types/parsimmon/index.d.ts(95,3): error TS2464: A computed property name must be of type 'string', 'number', 'symbol', or 'any'.
../node_modules/@types/parsimmon/index.d.ts(95,3): error TS7008: Member '[P in keyof' implicitly has an 'any' type.
../node_modules/@types/parsimmon/index.d.ts(95,4): error TS2304: Cannot find name 'P'.
../node_modules/@types/parsimmon/index.d.ts(95,9): error TS2304: Cannot find name 'keyof'.
../node_modules/@types/parsimmon/index.d.ts(95,15): error TS1005: ']' expected.
../node_modules/@types/parsimmon/index.d.ts(95,15): error TS2304: Cannot find name 'TLanguageSpec'.
../node_modules/@types/parsimmon/index.d.ts(95,28): error TS1005: ';' expected.
../node_modules/@types/parsimmon/index.d.ts(95,29): error TS1128: Declaration or statement expected.
../node_modules/@types/parsimmon/index.d.ts(95,38): error TS2304: Cannot find name 'TLanguageSpec'.
../node_modules/@types/parsimmon/index.d.ts(95,52): error TS2304: Cannot find name 'P'.
../node_modules/@types/parsimmon/index.d.ts(95,55): error TS1109: Expression expected.
../node_modules/@types/parsimmon/index.d.ts(96,2): error TS1128: Declaration or statement expected.
../node_modules/@types/parsimmon/index.d.ts(102,25): error TS2304: Cannot find name 'Result'.
../node_modules/@types/parsimmon/index.d.ts(216,18): error TS2304: Cannot find name 'Mark'.
../node_modules/@types/parsimmon/index.d.ts(220,49): error TS2304: Cannot find name 'Node'.
../node_modules/@types/parsimmon/index.d.ts(258,65): error TS2305: Module 'Parsimmon' has no exported member 'Reply'.
../node_modules/@types/parsimmon/index.d.ts(304,33): error TS2304: Cannot find name 'Rule'.
../node_modules/@types/parsimmon/index.d.ts(304,40): error TS2304: Cannot find name 'Language'.
../node_modules/@types/parsimmon/index.d.ts(305,48): error TS2304: Cannot find name 'TypedRule'.
../node_modules/@types/parsimmon/index.d.ts(398,49): error TS2304: Cannot find name 'Result'.
../node_modules/@types/parsimmon/index.d.ts(417,11): error TS7010: 'seqObj', which lacks return-type annotation, implicitly has an 'any' return type.
../node_modules/@types/parsimmon/index.d.ts(417,39): error TS1005: ',' expected.
../node_modules/@types/parsimmon/index.d.ts(417,39): error TS2300: Duplicate identifier 'T'.
../node_modules/@types/parsimmon/index.d.ts(417,41): error TS1005: ',' expected.
../node_modules/@types/parsimmon/index.d.ts(417,49): error TS1005: ',' expected.
../node_modules/@types/parsimmon/index.d.ts(417,49): error TS2300: Duplicate identifier 'T'.
../node_modules/@types/parsimmon/index.d.ts(417,82): error TS1005: ']' expected.
../node_modules/@types/parsimmon/index.d.ts(417,87): error TS1005: ')' expected.
../node_modules/@types/parsimmon/index.d.ts(417,89): error TS1109: Expression expected.
../node_modules/@types/parsimmon/index.d.ts(417,91): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.
../node_modules/@types/parsimmon/index.d.ts(417,91): error TS2365: Operator '<' cannot be applied to types '<T>(fn: (input: string, i: number) => any) => Parser<T>' and 'number'.
../node_modules/@types/parsimmon/index.d.ts(417,98): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.
../node_modules/@types/parsimmon/index.d.ts(417,103): error TS1109: Expression expected.
../node_modules/@types/parsimmon/index.d.ts(417,104): error TS1128: Declaration or statement expected.
../node_modules/@types/parsimmon/index.d.ts(417,115): error TS2464: A computed property name must be of type 'string', 'number', 'symbol', or 'any'.
../node_modules/@types/parsimmon/index.d.ts(417,116): error TS2304: Cannot find name 'K'.
../node_modules/@types/parsimmon/index.d.ts(417,121): error TS2304: Cannot find name 'Key'.
../node_modules/@types/parsimmon/index.d.ts(417,127): error TS2304: Cannot find name 'T'.
../node_modules/@types/parsimmon/index.d.ts(417,129): error TS2304: Cannot find name 'K'.
../node_modules/@types/parsimmon/index.d.ts(417,134): error TS1109: Expression expected.
../node_modules/@types/parsimmon/index.d.ts(439,39): error TS2304: Cannot find name 'StreamType'.
../node_modules/@types/parsimmon/index.d.ts(553,22): error TS2304: Cannot find name 'Index'.
../node_modules/@types/parsimmon/index.d.ts(578,99): error TS2464: A computed property name must be of type 'string', 'number', 'symbol', or 'any'.
../node_modules/@types/parsimmon/index.d.ts(578,100): error TS2304: Cannot find name 'K'.
../node_modules/@types/parsimmon/index.d.ts(578,105): error TS2304: Cannot find name 'Key'.
../node_modules/@types/parsimmon/index.d.ts(579,1): error TS1128: Declaration or statement expected.

I also tried adding "files": ["index.d.ts", "test.ts"] to types/tsconfig.json to only run against those files and "exclude": ["../node_modules"] and "exclude": ["node_modules"] but none worked for me.

Lastly, the current version of TS is 3.3 but going above 3.0 here errored with Error: Don't use // TypeScript Version for newer TS versions, use typesVerisons instead. It seems typeVersions isn't necessary currently since only a single version of types will be provided? (see here for more info on typeVersions)


/// <reference types="node" />

import * as fs from "fs";
import { EventEmitter } from "events";

/**
* The object's keys are all the directories (using absolute paths unless the `cwd` option was
* used), and the values are arrays of the names of the items contained in each directory.
*/
export interface WatchedPaths {
[directory: string]: string[];
}

export class FSWatcher extends EventEmitter implements fs.FSWatcher {
/**
* Constructs a new FSWatcher instance with optional WatchOptions parameter.
*/
constructor(options?: WatchOptions);

/**
* Add files, directories, or glob patterns for tracking. Takes an array of strings or just one
* string.
*/
add(paths: string | string[]): void;

/**
* Stop watching files, directories, or glob patterns. Takes an array of strings or just one
* string.
*/
unwatch(paths: string | string[]): void;

/**
* Returns an object representing all the paths on the file system being watched by this
* `FSWatcher` instance. The object's keys are all the directories (using absolute paths unless
* the `cwd` option was used), and the values are arrays of the names of the items contained in
* each directory.
*/
getWatched(): WatchedPaths;

/**
* Removes all listeners from watched files.
*/
close(): void;
}

export interface WatchOptions {
/**
* Indicates whether the process should continue to run as long as files are being watched. If
* set to `false` when using `fsevents` to watch, no more events will be emitted after `ready`,
* even if the process continues to run.
*/
persistent?: boolean;

/**
* ([anymatch](https://github.com/es128/anymatch)-compatible definition) Defines files/paths to
* be ignored. The whole relative or absolute path is tested, not just filename. If a function
* with two arguments is provided, it gets called twice per path - once with a single argument
* (the path), second time with two arguments (the path and the
* [`fs.Stats`](http://nodejs.org/api/fs.html#fs_class_fs_stats) object of that path).
*/
ignored?: any;

/**
* If set to `false` then `add`/`addDir` events are also emitted for matching paths while
* instantiating the watching as chokidar discovers these file paths (before the `ready` event).
*/
ignoreInitial?: boolean;

/**
* When `false`, only the symlinks themselves will be watched for changes instead of following
* the link references and bubbling events through the link's path.
*/
followSymlinks?: boolean;

/**
* The base directory from which watch `paths` are to be derived. Paths emitted with events will
* be relative to this.
*/
cwd?: string;

/**
* If set to true then the strings passed to .watch() and .add() are treated as literal path
* names, even if they look like globs. Default: false.
*/
disableGlobbing?: boolean;

/**
* Whether to use fs.watchFile (backed by polling), or fs.watch. If polling leads to high CPU
* utilization, consider setting this to `false`. It is typically necessary to **set this to
* `true` to successfully watch files over a network**, and it may be necessary to successfully
* watch files in other non-standard situations. Setting to `true` explicitly on OS X overrides
* the `useFsEvents` default.
*/
usePolling?: boolean;

/**
* Whether to use the `fsevents` watching interface if available. When set to `true` explicitly
* and `fsevents` is available this supercedes the `usePolling` setting. When set to `false` on
* OS X, `usePolling: true` becomes the default.
*/
useFsEvents?: boolean;

/**
* If relying upon the [`fs.Stats`](http://nodejs.org/api/fs.html#fs_class_fs_stats) object that
* may get passed with `add`, `addDir`, and `change` events, set this to `true` to ensure it is
* provided even in cases where it wasn't already available from the underlying watch events.
*/
alwaysStat?: boolean;

/**
* If set, limits how many levels of subdirectories will be traversed.
*/
depth?: number;

/**
* Interval of file system polling.
*/
interval?: number;

/**
* Interval of file system polling for binary files. ([see list of binary extensions](https://gi
* thub.com/sindresorhus/binary-extensions/blob/master/binary-extensions.json))
*/
binaryInterval?: number;

/**
* Indicates whether to watch files that don't have read permissions if possible. If watching
* fails due to `EPERM` or `EACCES` with this set to `true`, the errors will be suppressed
* silently.
*/
ignorePermissionErrors?: boolean;

/**
* `true` if `useFsEvents` and `usePolling` are `false`). Automatically filters out artifacts
* that occur when using editors that use "atomic writes" instead of writing directly to the
* source file. If a file is re-added within 100 ms of being deleted, Chokidar emits a `change`
* event rather than `unlink` then `add`. If the default of 100 ms does not work well for you,
* you can override it by setting `atomic` to a custom value, in milliseconds.
*/
atomic?: boolean | number;

/**
* can be set to an object in order to adjust timing params:
*/
awaitWriteFinish?: AwaitWriteFinishOptions | boolean;
}

export interface AwaitWriteFinishOptions {
/**
* Amount of time in milliseconds for a file size to remain constant before emitting its event.
*/
stabilityThreshold?: number;

/**
* File size polling interval.
*/
pollInterval?: number;
}

/**
* produces an instance of `FSWatcher`.
*/
export function watch(
paths: string | string[],
options?: WatchOptions
): FSWatcher;
60 changes: 60 additions & 0 deletions types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import * as fs from "fs";
import chokidar from "chokidar";

const watcher = chokidar.watch("file, dir, or glob", {
ignored: /[\/\\]\./,
persistent: true
});

const log = console.log.bind(console);

watcher
.on("add", (path: string) => {
log("File", path, "has been added");
})
.on("addDir", (path: string) => {
log("Directory", path, "has been added");
})
.on("change", (path: string) => {
log("File", path, "has been changed");
})
.on("unlink", (path: string) => {
log("File", path, "has been removed");
})
.on("unlinkDir", (path: string) => {
log("Directory", path, "has been removed");
})
.on("error", (error: any) => {
log("Error happened", error);
})
.on("ready", () => {
log("Initial scan complete. Ready for changes.");
})
.on("raw", (event: string, path: string, details: any) => {
log("Raw event info:", event, path, details);
});

// 'add', 'addDir' and 'change' events also receive stat() results as second
// argument when available: http://nodejs.org/api/fs.html#fs_class_fs_stats
watcher.on("change", (path: string, stats: fs.Stats) => {
if (stats) {
console.log("File", path, "changed size to", stats.size);
}
});

// Watch new files.
watcher.add("new-file");
watcher.add(["new-file-2", "new-file-3", "**/other-file*"]);

// Un-watch some files.
watcher.unwatch("new-file*");

// Only needed if watching is `persistent: true`.
watcher.close();

// One-liner
chokidar
.watch(".", { ignored: /[\/\\]\./ })
.on("all", (event: string, path: string) => {
console.log(event, path);
});
14 changes: 14 additions & 0 deletions types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noEmit": true,
"esModuleInterop": true,
"baseUrl": ".",
"paths": { "chokidar": ["."] }
}
}