Configurable type definitions (#24008)

* Configurable type definitionsπ

* Updated tests

* Added tests for errorsπ

* Updated project link

* Changed definitions to the proposed structure
This commit is contained in:
Vilim Stubičan
2018-03-01 23:29:57 +01:00
committed by Mohamed Hegazy
parent 82b9044020
commit 41b75d423c
4 changed files with 101 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
import configurable = require('configurable');
// Arrange
class A {
something(): void {
// nothing
}
}
const obj = new A();
const c = configurable(obj);
// $ExpectType A & Configurable<A>
c.set('first', 'first as a string');
// $ExpectType any
c.get('first');
// $ExpectType A & Configurable<A>
c.enable('first');
// $ExpectType boolean
c.enabled('first');
// $ExpectType A & Configurable<A>
c.disable('first');
// $ExpectType boolean
c.disabled('first');
// $ExpectError
c.set(5, 'first as a string');
// $ExpectError
c.set('first');
// $ExpectError
c.get(5);
// $ExpectError
c.enable(5);
// $ExpectError
c.enabled(5);
// $ExpectError
c.disable(5);
// $ExpectError
c.disabled(5);

27
types/configurable/index.d.ts vendored Normal file
View File

@@ -0,0 +1,27 @@
// Type definitions for configurable 0.0
// Project: https://www.npmjs.com/package/configurable
// Definitions by: Vilim Stubičan <https://github.com/jewbre>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.4
// Make any object configurable
declare function configurable<T extends object>(obj: T): T & Configurable<T>;
export = configurable;
interface Configurable<T> {
settings: {
[key: string]: any;
};
set(name: string, val: any): T & Configurable<T>;
get(name: string): any;
enable(name: string): T & Configurable<T>;
disable(name: string): T & Configurable<T>;
enabled(name: string): boolean;
disabled(name: string): boolean;
}

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",
"configurable-tests.ts"
]
}

View File

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