Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Project: Immutability helper
// TypeScript Version: 2.2

export = update

// declare function update<T>(data: T, query: update.Query<T>): T
declare function update<T>(
data: ReadonlyArray<T>,
query: ArrayOperators<T>,
): ReadonlyArray<T>

declare function update<T>(data: T, query: Query<T>): object

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be the last overload so that the ReadonlySet and ReadonlyMap ones are tried first.

declare function update<T>(
data: ReadonlySet<T>,
query: SetOperators<T>,
): ReadonlySet<T>

declare function update<K, V>(
data: ReadonlyMap<K, V>,
query: MapOperators<K, V>,
): ReadonlyMap<K, V>

type Tree<T> = {[K in keyof T]?: Query<T[K]>}
type Query<T> =
| Tree<T>
| ObjectOperators<T>
| ArrayOperators<any>
| SetOperators<any>

type ObjectOperators<T> =
| {$set: any}
| {$toggle: Array<keyof T>}
| {$unset: Array<keyof T>}
| {$merge: Partial<T>}
| {$apply: (old: T) => any}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be => T?

| ((old: T) => any)
type ArrayOperators<T> =
| {$push: T}
| {$unshift: T}
| {$splice: Array<[number, number]>}

type MapOperators<K, V> = {$add: Array<[K, V]>} | {$remove: K[]}
type SetOperators<T> = {$add: T[]} | {$remove: T[]}

declare namespace update {
function newContext(): typeof update
function extend<T>(
command: Command,
handler: (param: CommandArg, old: T) => T,
): void

type Command = string
type CommandArg = any
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"scripts": {
"test-cov": "nyc npm test && nyc report --reporter=lcov",
"test-travis": "nyc npm test && nyc report --reporter=lcov",
"test": "mocha test.js"
"test": "mocha test.js",
"test-dtslint": "dtslint"
},
"keywords": [
"immutability"
Expand All @@ -16,6 +17,7 @@
"license": "MIT",
"devDependencies": {
"coveralls": "^2.11.6",
"dtslint": "^0.2.0",
"expect": "^1.14.0",
"mocha": "^2.4.5",
"nyc": "^5.6.0"
Expand Down
15 changes: 15 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"noEmit": true,
"strictFunctionTypes": false,

"baseUrl": ".",
"paths": { "immutability-helper": ["."] }
}
}

7 changes: 7 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"semicolon": false
}
}