DefinitelyTyped/types/lowdb/index.d.ts
Nathan Shively-Sanders e4f9fd1856
Make lodash faster (#35241)
* MINIFY

* MINIFY 2

* MINIFY 3

* MINIFY 4

* switch to unknown (-3500 types)

* Switch to built-in Omit

* Simplify differenceBy/differenceWith

I only changed the Imp/Exp overloads, because those are the ones that
create significant numbers of types.

The normal case is still fully typed, but when passing multiple
comparison arrays, they are all typed as `List<unknown>`, and the
iteratee is not checked for compatibility with their types.

You should never use multiple comparison arrays.

Brings type count down 18k, from 452k to 434k.

* Simplify fill

Again, only touches Imp/Exp.

The only semantic difference is that, for the uncommon case that you
fill an array of type T with elements of type U, the special-case for
filling the whole array is gone. You get Array<T | U> now, not Array<U>.

You should never change the type of an array with _.fill.

* Simplify union/intersection[By/With]

In the same way as difference.

Saves 15k, from 426k to 411k.

* Simplify pullAll/xor[By/With]

411k -> 408k

* Simplify zip[By/With]

408 -> 381k types

* each[Right] is a synonym for forEach[Right]

Doesn't make a significant difference in the number of types though.

* MINIFY 5

* Simplify List | NumericDictionary unions by removing List

They are almost the same type.
381k -> 380k

* Clean up function overloads

381k -> 328k

Still not enough for dtslint to succeed in 1.4 GB of memory. :(

* clean up lang.d.ts

* I don't even know what's happening.

Large swatchs of functions are disabled. The conversion of methods to
list-only seems to help, but I'm not sure this will hold for other
methods that apply to other types.

* fix type parameter name

* Reform array

Things are moving in the right direction!
2,351k types

* Better type parameters on Imp/Exp subtypes

2,329k types

* Actually correct base type parameters for ImpL/ExpL

* every/filter/find/findLast

2,037k types

* flatMap[Deep/Depth]

1,865k types

* forEach[Right], groupBy

1,822k types

* includes, keyBy, map

1,653k types

* fix wrapper constructors; add partition,orderBy

back up to 1,733k types :(

* Finish collection.d.ts

1,424k types

I'm not sure this thing is going to converge at a lower value than the
old code.

* Update ImpF interface and some of function.d.ts

 1382k types

* clone, castArray to base, ImpU<T> is now generic

1,412k types, which moves backward a bit actually.

* Rest of lang.d.ts

1,378k types

* Finish math.d.ts

1337k types

* Restore commented-out functions

Still kind of broken though. More updates tomorrow.

* Fix definition of Imp/ExpO

* any out really big overload sets

down to 385k types

* Lots more fixed

272k types

* omit, pick[By], others

181k types

* Reenable some things, fix transforms types, fix values[In]

157k types

* Fix rest of errors

Doesn't fix $Expect assertions yet!

165k types

* Remove more this-types

94k types

* Remove rest of this types

Well, almost all. There are still a few ones that didn't save much or
any perf.

75k types

* Better backward compatibility

* Fix lots of lint

Lots of lint to go!

* Fix more lints

Still about 2500 to go!

* Fix ExpectType, part 1

13% done

* More fixes from ExpectType

33% done

* More fixes

41% done or so?

* Done up to curry

49%

* More expect rule fixes

71% done

* Done through mapValues

80% done

* Partly done pickyBy

82% done, almost

* Restore partial/partialRight

and get rid of rest of `this` parameters

* More fixes

87% done

* More test fixes

97% done

* Last round of test fixes

...Maybe. There are some weird failures on my laptop that don't match
what my language service is telling me.

* Final round of test fixes

Fixes Partial<T> from yesterday plus a couple that cropped up afterward.

* Get rid of bare Imp/Exp returns

Add ImpChain/ExpChain, but it's expensive so shouldn't be used much.

* Make object methods specific to object.

Saves about 8000 types.

* Start converting remain Exp uses

So far a few of them need to be ExpChain.

* Remove rest of Exp<T> returns

Not all are gone, but most are. A few explode performance, and a few
don't have a sensible replacement.

* Move changes into ts3.1 and revert to old top-level

* Rename to old names

* Fix test resolution

With absolute paths, module resolution picks up types/lodash/fp.d.ts
instead of types/lodash/ts3.1/fp.d.ts.

* Update fp.d.ts

* Add tslint and rename expectrule assertions

* Remove lowdb codegen

Use a worse type for `write` instead. It is not worth the complexity for a
rarely-used package, and the return type is rarely used, except to await
it.

* Remove list methods that should not be on ExpU

* Change names to match Lodash module names

* Update names in lowdb too

* Restore trivial jsdocs

Which is the majority of them.

I still need to restore

1. All the specific jsdocs on LodashStatic functions.
2. All the jsdocs in common.d.ts
3. All the jsdocs on various non-function properties in various files.

* Restore jsdoc to common.d.ts

* Restore head jsdocs

* Improve `any` handling and pop's type

The latter is a bit expensive but probably worth it?

* Update tests

* Add StringNullableChain and more ExpChain returns

* Update lowdb tests

They need a cast now that nullability is tracked correctly!

* First round of review changes

* Remove generate-lowdb, fix Object.iteratee type

* Update test assertion

* Update test again

Not sure why this didn't fail on the previous run
2019-05-09 08:59:05 -07:00

270 lines
8.8 KiB
TypeScript

// Type definitions for Lowdb 1.0
// Project: https://github.com/typicode/lowdb
// Definitions by: typicode <https://github.com/typicode>
// Bazyli Brzóska <https://github.com/niieani>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.1
import { LoDashStatic, ObjectChain } from "lodash";
declare module "lodash" {
interface ObjectChain<T> {
/**
* @description Be careful: This function overwrites the whole database.
*/
write(): T & Promise<T>;
}
interface PrimitiveChain<T> {
write(): T & Promise<T>;
}
interface CollectionChain<T> {
write(): ArrayLike<T> & Promise<ArrayLike<T>>;
}
interface FunctionChain<T> {
write(): T & Promise<T>;
}
interface StringChain {
write(): string & Promise<string>;
}
}
declare let Lowdb: Lowdb.lowdb;
export = Lowdb;
declare namespace Lowdb {
interface AdapterOptions<SchemaT = any> {
defaultValue?: SchemaT;
serialize?: (data: SchemaT) => string;
deserialize?: (serializedData: string) => SchemaT;
}
interface BaseAdapter<SchemaT = any> extends AdapterOptions<SchemaT> {
readonly "@@reference": SchemaT;
new <SchemaT = any>(
source: string,
options?: AdapterOptions<SchemaT>
): BaseAdapter<SchemaT>;
source: string;
}
interface AdapterSync<SchemaT = any> extends BaseAdapter<SchemaT> {
new <SchemaT = any>(
source: string,
options?: AdapterOptions<SchemaT>
): AdapterSync<SchemaT>;
write(state: object): void;
}
interface AdapterAsync<SchemaT = any> extends BaseAdapter<SchemaT> {
new <SchemaT = any>(
source: string,
options?: AdapterOptions<SchemaT>
): AdapterAsync<SchemaT>;
write(state: object): Promise<void>;
}
interface LowdbBase<SchemaT> {
getState: () => SchemaT;
setState: (state: SchemaT) => this;
}
interface LowdbSync<SchemaT> extends LowdbBase<SchemaT>, ObjectChain<SchemaT> {
_: LoDashStatic;
read: () => this;
/**
* @description Be careful: This function overwrites the whole database.
*/
write<T = void>(returnValue?: T): T & Promise<T>;
}
interface LowdbAsync<SchemaT> extends LowdbBase<SchemaT>, ObjectChain<SchemaT> {
_: LoDashStatic;
read: () => Promise<this>;
/**
* @description Be careful: This function overwrites the whole database.
*/
write<T = void>(returnValue?: T): T & Promise<T>;
}
interface LowdbFpSync<SchemaT> extends LowdbBase<SchemaT> {
/**
* @description Be careful: This function overwrites the whole database.
*/
write<T = void>(returnValue?: T): T;
/**
* @description Returns a function that allows you to access/modify the database at a given path.
* @example
* ```js
* const posts = db('posts')
* const firstPost = posts(all => all[0])
* posts.write((allPosts) => [...allPosts, {title: 'Yup!'}])
* ```
*/
<TKey extends keyof SchemaT>(
path: TKey | [TKey],
defaultValue?: SchemaT[TKey]
): FpReturnSync<SchemaT[TKey]>;
<TKey extends keyof SchemaT, TSubKey extends keyof SchemaT[TKey]>(
path: [TKey, TSubKey],
defaultValue?: SchemaT[TKey][TSubKey]
): FpReturnSync<SchemaT[TKey][TSubKey]>;
<
TKey extends keyof SchemaT,
TSubKey extends keyof SchemaT[TKey],
TSubKey2 extends keyof SchemaT[TKey][TSubKey]
>(
path: [TKey, TSubKey, TSubKey2],
defaultValue?: SchemaT[TKey][TSubKey][TSubKey2]
): FpReturnSync<SchemaT[TKey][TSubKey][TSubKey2]>;
<
TKey extends keyof SchemaT,
TSubKey extends keyof SchemaT[TKey],
TSubKey2 extends keyof SchemaT[TKey][TSubKey],
TSubKey3 extends keyof SchemaT[TKey][TSubKey][TSubKey2]
>(
path: [TKey, TSubKey, TSubKey2, TSubKey3],
defaultValue?: SchemaT[TKey][TSubKey][TSubKey2][TSubKey3]
): FpReturnSync<SchemaT[TKey][TSubKey][TSubKey2][TSubKey3]>;
<
TKey extends keyof SchemaT,
TSubKey extends keyof SchemaT[TKey],
TSubKey2 extends keyof SchemaT[TKey][TSubKey],
TSubKey3 extends keyof SchemaT[TKey][TSubKey][TSubKey2],
TSubKey4 extends keyof SchemaT[TKey][TSubKey][TSubKey2][TSubKey3]
>(
path: [TKey, TSubKey, TSubKey2, TSubKey3, TSubKey4],
defaultValue?: SchemaT[TKey][TSubKey][TSubKey2][TSubKey3][TSubKey4]
): FpReturnSync<SchemaT[TKey][TSubKey][TSubKey2][TSubKey3][TSubKey4]>;
<T = any>(path: string | string[], defaultValue?: T): FpReturnSync<T>;
}
interface LowdbFpAsync<SchemaT> extends LowdbBase<SchemaT> {
/**
* @description Be careful: This function overwrites the whole database.
*/
write<T = void>(returnValue?: T): Promise<T>;
/**
* @description Returns a function that allows you to access/modify the database at a given path.
* @example
* ```js
* const posts = db('posts')
* const firstPost = posts(all => all[0])
* posts.write((allPosts) => [...allPosts, {title: 'Yup!'}])
* ```
*/
<TKey extends keyof SchemaT>(
path: TKey | [TKey],
defaultValue?: SchemaT[TKey]
): FpReturnAsync<SchemaT[TKey]>;
<TKey extends keyof SchemaT, TSubKey extends keyof SchemaT[TKey]>(
path: [TKey, TSubKey],
defaultValue?: SchemaT[TKey][TSubKey]
): FpReturnAsync<SchemaT[TKey][TSubKey]>;
<
TKey extends keyof SchemaT,
TSubKey extends keyof SchemaT[TKey],
TSubKey2 extends keyof SchemaT[TKey][TSubKey]
>(
path: [TKey, TSubKey, TSubKey2],
defaultValue?: SchemaT[TKey][TSubKey][TSubKey2]
): FpReturnAsync<SchemaT[TKey][TSubKey][TSubKey2]>;
<
TKey extends keyof SchemaT,
TSubKey extends keyof SchemaT[TKey],
TSubKey2 extends keyof SchemaT[TKey][TSubKey],
TSubKey3 extends keyof SchemaT[TKey][TSubKey][TSubKey2]
>(
path: [TKey, TSubKey, TSubKey2, TSubKey3],
defaultValue?: SchemaT[TKey][TSubKey][TSubKey2][TSubKey3]
): FpReturnAsync<SchemaT[TKey][TSubKey][TSubKey2][TSubKey3]>;
<
TKey extends keyof SchemaT,
TSubKey extends keyof SchemaT[TKey],
TSubKey2 extends keyof SchemaT[TKey][TSubKey],
TSubKey3 extends keyof SchemaT[TKey][TSubKey][TSubKey2],
TSubKey4 extends keyof SchemaT[TKey][TSubKey][TSubKey2][TSubKey3]
>(
path: [TKey, TSubKey, TSubKey2, TSubKey3, TSubKey4],
defaultValue?: SchemaT[TKey][TSubKey][TSubKey2][TSubKey3][TSubKey4]
): FpReturnAsync<SchemaT[TKey][TSubKey][TSubKey2][TSubKey3][TSubKey4]>;
<T = any>(path: string | string[], defaultValue?: T): FpReturnAsync<T>;
}
interface FpReturnBase<PathT> {
/**
* Execute a series of functions on the data at a given path.
* Result of previous function is the input of the next one.
* Returns the result of the last function.
*/
<R1>(f1: (a1: PathT) => R1): R1;
// <R1>(f1: [(a1: PathT) => R1]): R1;
<R1, R2>(f1: [(a1: PathT) => R1, (a: R1) => R2]): R2;
<R1, R2, R3>(f1: [(a1: PathT) => R1, (a: R1) => R2, (a: R2) => R3]): R3;
<R1, R2, R3, R4>(
f1: [(a1: PathT) => R1, (a: R1) => R2, (a: R2) => R3, (a: R3) => R4]
): R4;
<R1, R2, R3, R4, R5>(
f1: [
(a1: PathT) => R1,
(a: R1) => R2,
(a: R2) => R3,
(a: R3) => R4,
(a: R4) => R5
]
): R5;
<R1, R2, R3, R4, R5, R6>(
f1: [
(a1: PathT) => R1,
(a: R1) => R2,
(a: R2) => R3,
(a: R3) => R4,
(a: R4) => R5,
(a: R5) => R6
]
): R6;
<R1, R2, R3, R4, R5, R6, R7>(
f1: [
(a1: PathT) => R1,
(a: R1) => R2,
(a: R2) => R3,
(a: R3) => R4,
(a: R4) => R5,
(a: R5) => R6,
(a: R6) => R7
]
): R7;
(funcs: Array<(a: any) => any>): any;
}
interface FpReturnSync<PathT> extends FpReturnBase<PathT> {
/**
* @description Writes the change to the database, based on the callback's return value.
* @example
* ```js
* posts.write((allPosts) => [...allPosts, {title: 'Yup!'}])
* ```
*/
write<R1 extends PathT>(f1: (a1: PathT) => R1): R1;
}
interface FpReturnAsync<PathT> extends FpReturnBase<PathT> {
/**
* @description Writes the change to the database, based on the callback's return value.
* @example
* ```js
* posts.write((allPosts) => [...allPosts, {title: 'Yup!'}])
* ```
*/
write<R1 extends PathT>(f1: (a1: PathT) => R1): Promise<R1>;
}
interface lowdb {
<AdapterT extends AdapterAsync>(adapter: AdapterT): Promise<LowdbAsync<AdapterT[ReferenceProperty]>>;
<AdapterT extends AdapterSync>(adapter: AdapterT): LowdbSync<AdapterT[ReferenceProperty]>;
}
interface lowdbFp {
<AdapterT extends AdapterAsync>(adapter: AdapterT): Promise<LowdbFpAsync<AdapterT[ReferenceProperty]>>;
<AdapterT extends AdapterSync>(adapter: AdapterT): LowdbFpSync<AdapterT[ReferenceProperty]>;
}
}
type ReferenceProperty = "@@reference";