mirror of
https://github.com/wassname/DefinitelyTyped.git
synced 2026-09-09 11:13:57 +08:00
Merge pull request #5819 from tkqubo/core-decorators.js
Add core-decorators.js
This commit is contained in:
@@ -0,0 +1,169 @@
|
||||
/// <reference path="core-decorators.d.ts" />
|
||||
|
||||
//
|
||||
// @autobind
|
||||
//
|
||||
|
||||
import { autobind } from 'core-decorators';
|
||||
|
||||
class Person {
|
||||
@autobind
|
||||
getPerson() {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
let person = new Person();
|
||||
let getPerson = person.getPerson;
|
||||
|
||||
getPerson() === person;
|
||||
|
||||
//
|
||||
// @readonly
|
||||
//
|
||||
|
||||
import { readonly } from 'core-decorators';
|
||||
|
||||
class Meal {
|
||||
@readonly
|
||||
entree: string = 'steak';
|
||||
}
|
||||
|
||||
var dinner = new Meal();
|
||||
dinner.entree = 'salmon';
|
||||
|
||||
//
|
||||
// @override
|
||||
//
|
||||
|
||||
import { override } from 'core-decorators';
|
||||
|
||||
class Parent {
|
||||
speak(first: string, second: string) {}
|
||||
}
|
||||
|
||||
class Child extends Parent {
|
||||
@override
|
||||
speak() {}
|
||||
// SyntaxError: Child#speak() does not properly override Parent#speak(first, second)
|
||||
}
|
||||
|
||||
// or
|
||||
|
||||
class Child2 extends Parent {
|
||||
@override
|
||||
speaks() {}
|
||||
// SyntaxError: No descriptor matching Child#speaks() was found on the prototype chain.
|
||||
//
|
||||
// Did you mean "speak"?
|
||||
}
|
||||
|
||||
//
|
||||
// @deprecate (alias: @deprecated)
|
||||
//
|
||||
|
||||
import { deprecate, deprecated } from 'core-decorators';
|
||||
|
||||
class Person2 {
|
||||
@deprecate
|
||||
facepalm() {}
|
||||
|
||||
@deprecate('We stopped facepalming')
|
||||
facepalmHard() {}
|
||||
|
||||
@deprecate('We stopped facepalming', { url: 'http://knowyourmeme.com/memes/facepalm' })
|
||||
facepalmHarder() {}
|
||||
}
|
||||
|
||||
let person2 = new Person2();
|
||||
|
||||
person2.facepalm();
|
||||
// DEPRECATION Person#facepalm: This function will be removed in future versions.
|
||||
|
||||
person2.facepalmHard();
|
||||
// DEPRECATION Person#facepalmHard: We stopped facepalming
|
||||
|
||||
person2.facepalmHarder();
|
||||
// DEPRECATION Person#facepalmHarder: We stopped facepalming
|
||||
//
|
||||
// See http://knowyourmeme.com/memes/facepalm for more details.
|
||||
//
|
||||
|
||||
//
|
||||
// @debounce
|
||||
//
|
||||
|
||||
import { debounce } from 'core-decorators';
|
||||
|
||||
class Editor {
|
||||
|
||||
content = '';
|
||||
|
||||
@debounce(500)
|
||||
updateContent(content: string) {
|
||||
this.content = content;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// @suppressWarnings
|
||||
//
|
||||
|
||||
import { suppressWarnings } from 'core-decorators';
|
||||
|
||||
class Person3 {
|
||||
@deprecated
|
||||
facepalm() {}
|
||||
|
||||
@suppressWarnings
|
||||
facepalmWithoutWarning() {
|
||||
this.facepalm();
|
||||
}
|
||||
}
|
||||
|
||||
let person3 = new Person3();
|
||||
|
||||
person3.facepalmWithoutWarning();
|
||||
// no warning is logged
|
||||
|
||||
//
|
||||
// @nonenumerable
|
||||
//
|
||||
|
||||
import { nonenumerable } from 'core-decorators';
|
||||
|
||||
class Meal2 {
|
||||
entree = 'steak';
|
||||
|
||||
@nonenumerable
|
||||
cost: number = 4.44;
|
||||
}
|
||||
|
||||
var dinner2 = new Meal2();
|
||||
for (var key in dinner2) {
|
||||
key;
|
||||
// "entree" only, not "cost"
|
||||
}
|
||||
|
||||
Object.keys(dinner2);
|
||||
// ["entree"]
|
||||
|
||||
//
|
||||
// @nonconfigurable
|
||||
//
|
||||
|
||||
import { nonconfigurable } from 'core-decorators';
|
||||
|
||||
class Meal3 {
|
||||
@nonconfigurable
|
||||
entree: string = 'steak';
|
||||
}
|
||||
|
||||
var dinner3 = new Meal3();
|
||||
|
||||
Object.defineProperty(dinner3, 'entree', {
|
||||
enumerable: false
|
||||
});
|
||||
// Cannot redefine property: entree
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
--experimentalDecorators --noImplicitAny --target ES5
|
||||
Vendored
+89
@@ -0,0 +1,89 @@
|
||||
// Type definitions for core-decorators.js v0.1.5
|
||||
// Project: https://github.com/jayphelps/core-decorators.js
|
||||
// Definitions by: Qubo <https://github.com/tkqubo>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
declare module "core-decorators" {
|
||||
export interface ClassDecorator {
|
||||
<TFunction extends Function>(target: TFunction): TFunction|void;
|
||||
}
|
||||
|
||||
export interface ParameterDecorator {
|
||||
(target: Object, propertyKey: string|symbol, parameterIndex: number): void;
|
||||
}
|
||||
|
||||
export interface PropertyDecorator {
|
||||
(target: Object, propertyKey: string|symbol): void;
|
||||
}
|
||||
|
||||
export interface MethodDecorator {
|
||||
<T>(target: Object, propertyKey: string|symbol, descriptor: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T>|void;
|
||||
}
|
||||
|
||||
export interface PropertyOrMethodDecorator extends MethodDecorator, PropertyDecorator {
|
||||
(target: Object, propertyKey: string|symbol): void;
|
||||
}
|
||||
|
||||
export interface Deprecate extends MethodDecorator {
|
||||
(message?: string, option?: DeprecateOption): MethodDecorator;
|
||||
}
|
||||
|
||||
export interface DeprecateOption {
|
||||
url: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Forces invocations of this function to always have this refer to the class instance,
|
||||
* even if the function is passed around or would otherwise lose its this context. e.g. var fn = context.method;
|
||||
*/
|
||||
var autobind: MethodDecorator;
|
||||
/**
|
||||
* Marks a property or method as not being writable.
|
||||
*/
|
||||
var readonly: PropertyOrMethodDecorator;
|
||||
/**
|
||||
* Checks that the marked method indeed overrides a function with the same signature somewhere on the prototype chain.
|
||||
*/
|
||||
var override: MethodDecorator;
|
||||
/**
|
||||
* Calls console.warn() with a deprecation message. Provide a custom message to override the default one. You can also provide an options hash with a url, for further reading.
|
||||
*/
|
||||
var deprecate: Deprecate;
|
||||
/**
|
||||
* Calls console.warn() with a deprecation message. Provide a custom message to override the default one. You can also provide an options hash with a url, for further reading.
|
||||
*/
|
||||
var deprecated: Deprecate;
|
||||
/**
|
||||
* Creates a new debounced function which will be invoked after wait milliseconds since the time it was invoked. Default timeout is 300 ms.
|
||||
*/
|
||||
var debounce: (wait: number) => MethodDecorator;
|
||||
/**
|
||||
* Suppresses any JavaScript console.warn() call while the decorated function is called. (i.e. on the stack)
|
||||
*/
|
||||
var suppressWarnings: MethodDecorator;
|
||||
/**
|
||||
* Marks a property or method as not being enumerable.
|
||||
*/
|
||||
var nonenumerable: PropertyOrMethodDecorator;
|
||||
/**
|
||||
* Marks a property or method as not being writable.
|
||||
*/
|
||||
var nonconfigurable: PropertyOrMethodDecorator;
|
||||
/**
|
||||
* Initial implementation included, likely slow. WIP.
|
||||
*/
|
||||
var memoize: MethodDecorator;
|
||||
|
||||
export {
|
||||
autobind,
|
||||
readonly,
|
||||
override,
|
||||
deprecate,
|
||||
deprecated,
|
||||
debounce,
|
||||
suppressWarnings,
|
||||
nonenumerable,
|
||||
nonconfigurable,
|
||||
memoize // WIP
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user