Add types for cycled

This commit is contained in:
Dimitri Benin
2018-12-09 15:28:37 +01:00
parent e95e11dcf2
commit 0a6ead5aee
4 changed files with 135 additions and 0 deletions
+53
View File
@@ -0,0 +1,53 @@
import Cycled = require('cycled');
const cycled = new Cycled([1, 2, 3]);
// $ExpectType Cycled<number>
cycled;
// $ExpectType number
cycled.index;
cycled.index = 1;
// $ExpectType number
cycled.current();
// $ExpectType number
cycled.next();
// $ExpectType number
cycled.previous();
// $ExpectType number
cycled.step(10);
// $ExpectType Iterator<number>
cycled.indefinitely();
// $ExpectType Iterator<number>
cycled.indefinitelyReversed();
// $ExpectType number[]
[...cycled];
class TabComponent {
views: Cycled<string>;
activeView: string;
constructor(views: string[]) {
this.activeView = views[0];
this.views = new Cycled(views);
}
setActiveView(view: string) {
this.activeView = view;
this.views.index = this.views.indexOf(view);
}
nextView() {
this.setActiveView(this.views.next());
}
previousView() {
this.setActiveView(this.views.previous());
}
}
+58
View File
@@ -0,0 +1,58 @@
// Type definitions for cycled 1.0
// Project: https://github.com/sindresorhus/cycled#readme
// Definitions by: BendingBender <https://github.com/BendingBender>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
export = Cycled;
// tslint:disable:jsdoc-format
declare class Cycled<T> extends Array<T> {
/**
* Initiates an array subclass with the methods documented below.
* Since it's an array, you can use all the normal array methods on it.
*
* The instance is an iterable that will cycle through the array.
* It will cycle through the number of elements equaling the length of the array from the current index.
* ```
const numberCycle = new Cycled([1,2,3,4,5]);
console.log(...numberCycle);
//=> 1 2 3 4 5
```
*
* @param input
*/
constructor(input: T[]);
/**
* Get or set the current index.
*/
index: number;
/**
* Returns the current item.
*/
current(): T;
/**
* Returns the next item.
*/
next(): T;
/**
* Returns the previous item.
*/
previous(): T;
/**
* Returns the item by going the given amount of `steps` through the array.
* For example, calling `step(2)` is like calling `next()` twice. You go backward by specifying a negative number.
* @param steps
*/
step(steps: number): T;
/**
* Returns an iterable that will cycle through the array indefinitely.
*/
indefinitely(): Iterator<T>;
/**
* Returns an iterable that will cycle through the array backward indefinitely.
*/
indefinitelyReversed(): Iterator<T>;
}
+23
View File
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"cycled-tests.ts"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }