core-decorators: Provides its own types (#22349)

This commit is contained in:
Andy
2018-01-02 09:29:03 -08:00
committed by GitHub
parent 9fa88b517f
commit a43a5ddeb0
5 changed files with 6 additions and 446 deletions
+6
View File
@@ -216,6 +216,12 @@
"sourceRepoURL": "https://github.com/blakeembrey/constant-case",
"asOfVersion": "1.1.2"
},
{
"libraryName": "core-decorators.js",
"typingsPackageName": "core-decorators",
"sourceRepoURL": "https://github.com/jayphelps/core-decorators.js",
"asOfVersion": "0.20.0"
},
{
"libraryName": "cypress",
"typingsPackageName": "cypress",
@@ -1,295 +0,0 @@
//
// @autobind
//
import { autobind } from 'core-decorators';
@autobind
class Person {
@autobind
getPerson() {
return this;
}
getPersonAgain() {
return this;
}
}
const person = new Person();
const { getPerson, getPersonAgain } = person;
getPerson() === person;
getPersonAgain() === person;
//
// @readonly
//
import { readonly } from 'core-decorators';
class Meal {
@readonly
entree = 'steak';
}
const 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() {}
}
const 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;
}
}
//
// @throttle
//
import { throttle } from 'core-decorators';
class Editor2 {
content = '';
@throttle(500, {leading: false})
updateContent(content: string) {
this.content = content;
}
}
//
// @suppressWarnings
//
import { suppressWarnings } from 'core-decorators';
class Person3 {
@deprecated
facepalm() {}
@suppressWarnings
facepalmWithoutWarning() {
this.facepalm();
}
}
const person3 = new Person3();
person3.facepalmWithoutWarning();
// no warning is logged
//
// @nonenumerable
//
import { nonenumerable } from 'core-decorators';
class Meal2 {
entree = 'steak';
@nonenumerable
cost = 4.44;
}
const dinner2 = new Meal2();
for (const key in dinner2) {
key;
// "entree" only, not "cost"
}
Object.keys(dinner2);
// ["entree"]
//
// @nonconfigurable
//
import { nonconfigurable } from 'core-decorators';
class Meal3 {
@nonconfigurable
entree = 'steak';
}
const dinner3 = new Meal3();
Object.defineProperty(dinner3, 'entree', {
enumerable: false
});
// Cannot redefine property: entree
//
// @decorate
//
declare function memoize<T extends Function>(func: T): T;
import { decorate } from 'core-decorators';
let count = 0;
class Task {
@decorate(memoize)
doSomethingExpensive(data: any) {
count++;
// something expensive;
return data;
}
}
const task = new Task();
const data = [1, 2, 3];
task.doSomethingExpensive(data);
task.doSomethingExpensive(data);
count === 1;
// true
//
// @lazyInitialize
//
import { lazyInitialize } from 'core-decorators';
function createHugeBuffer() {
console.log('huge buffer created');
return new Array(1000000);
}
class Editor3 {
@lazyInitialize
hugeBuffer = createHugeBuffer();
}
const editor = new Editor3();
// createHugeBuffer() has not been called yet
editor.hugeBuffer;
// logs 'huge buffer created', now it has been called
editor.hugeBuffer;
// already initialized and equals our buffer, so
// createHugeBuffer() is not called again
// TODO: For @mixin, I don't know how we can make it work for TypeScript...
//
// @mixin (alias: @mixins)
//
import { mixin } from 'core-decorators';
const SingerMixin = {
sing(sound: string) {
alert(sound);
}
};
const FlyMixin = {
// All types of property descriptors are supported
get speed(): number { return 42; },
fly() {},
land() {}
};
@mixin(SingerMixin, FlyMixin)
class Bird {
singMatingCall() {
// TODO: For @mixin, I don't know how we can make it work for TypeScript...
// this.sing('tweet tweet');
}
}
const bird = new Bird();
bird.singMatingCall();
// alerts "tweet tweet"
//
// @time
//
import { time } from 'core-decorators';
const myConsole = {
time(label: string) { /* custom time() method */ },
timeEnd(label: string) { /* custom timeEnd method */ },
log(str: any) { /* custom log method */ }
};
class Bird2 {
@time('sing')
sing() {}
@time('sing2', myConsole)
sing2() {}
}
const bird2 = new Bird2();
bird2.sing(); // console.time label will be 'sing-0'
bird2.sing(); // console.time label will be 'sing-1'
-116
View File
@@ -1,116 +0,0 @@
// Type definitions for core-decorators.js 0.10
// Project: https://github.com/jayphelps/core-decorators.js
// Definitions by: Qubo <https://github.com/tkqubo>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.1
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;
}
export interface ThrottleOptions {
/** allows to trigger function on the leading. */
leading?: boolean;
/** allows to trigger function on the trailing edge of the wait interval. */
trailing?: boolean;
}
export interface Console {
log(message?: any, ...optionalParams: any[]): void;
time(timerName?: string): void;
timeEnd(timerName?: string): void;
}
/**
* 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;
*/
export const autobind: Function;
/**
* Marks a property or method as not being writable.
*/
export const readonly: PropertyOrMethodDecorator;
/**
* Checks that the marked method indeed overrides a function with the same signature somewhere on the prototype chain.
*/
export const 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.
*/
export const 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.
*/
export const 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.
*/
export function debounce(wait: number): MethodDecorator;
/**
* Creates a new throttled function which will be invoked in every wait milliseconds. Default timeout is 300 ms.
*/
export function throttle(wait: number, options?: ThrottleOptions): MethodDecorator;
/**
* Suppresses any JavaScript console.warn() call while the decorated function is called. (i.e. on the stack)
*/
export const suppressWarnings: MethodDecorator;
/**
* Marks a property or method as not being enumerable.
*/
export const nonenumerable: PropertyOrMethodDecorator;
/**
* Marks a property or method as not being writable.
*/
export const nonconfigurable: PropertyOrMethodDecorator;
/**
* Initial implementation included, likely slow. WIP.
*/
export const memoize: MethodDecorator;
/**
* Immediately applies the provided function and arguments to the method, allowing you to wrap methods with arbitrary helpers like those provided by lodash.
* The first argument is the function to apply, all further arguments will be passed to that decorating function.
*/
export function decorate(func: Function, ...args: any[]): MethodDecorator;
/**
* Prevents a property initializer from running until the decorated property is actually looked up.
* Useful to prevent excess allocations that might otherwise not be used, but be careful not to over-optimize things.
*/
export const lazyInitialize: PropertyDecorator;
/**
* Mixes in all property descriptors from the provided Plain Old JavaScript Objects (aka POJOs) as arguments.
* Mixins are applied in the order they are passed, but do not override descriptors already on the class, including those inherited traditionally.
*/
export function mixin(...mixins: any[]): ClassDecorator;
/**
* Mixes in all property descriptors from the provided Plain Old JavaScript Objects (aka POJOs) as arguments.
* Mixins are applied in the order they are passed, but do not override descriptors already on the class, including those inherited traditionally.
*/
export function mixins(...mixins: any[]): ClassDecorator;
/**
* Uses console.time and console.timeEnd to provide function timings with a unique label whose default prefix is ClassName.method. Supply a first argument to override the prefix:
*/
export function time(label: string, console?: Console): MethodDecorator;
-26
View File
@@ -1,26 +0,0 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"strictFunctionTypes": true,
"baseUrl": "../",
"experimentalDecorators": true,
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"core-decorators-tests.ts"
]
}
-9
View File
@@ -1,9 +0,0 @@
{
"extends": "dtslint/dt.json",
"rules": {
// TODOs
"callable-types": false,
"ban-types": false,
"no-duplicate-imports": false
}
}