Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add typescript definitions #13

Open
wants to merge 4 commits into
base: latest
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,24 @@ const MyAppOpts = figgyPudding({
})
```

##### `opts`

###### `other(key: String) -> Boolean`

Additional keys can be allowed, if they pass the test in this function

###### Example

```javascript
const MyOtherOpts = puddin({
a: {}
}, {
other (key) { return /^special-/.test(key) }
})
```

This will allow values that start with `special-`

#### <a name="pudding-factory"></a> `> PuddingFactory(...providers) -> FiggyPudding{}`

Instantiates an options object defined by `figgyPudding()`, which uses
Expand Down
72 changes: 72 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

declare function figgyPudding<
S extends figgyPudding.Specs = {},
O extends figgyPudding.Options = {}
>(specs?: S, opts?: O): figgyPudding.PuddingFactory<S, O>

declare module figgyPudding {
interface Options {
other?(key: string): boolean
}

type OtherOpt = Required<Pick<Options, 'other'>>

type Specs = {
[K in string]: string | Spec
}
interface Spec {
default?: any
}

type SpecWithDefault = Required<Pick<Spec, 'default'>>
type WidenPrimitive<T> =
T extends string ? string :
T extends number ? number :
T extends boolean ? boolean :
T
type SpecDefault<S> = S extends {default(): infer R} ? WidenPrimitive<R> : S extends {default: infer D} ? D : unknown

interface MapLike<K, V> {
get(key: K): V | undefined
}

type AvailableKeys<S, O> = O extends OtherOpt ? string : keyof S

type Proxy<S, O> = {
[K in keyof S]: SpecDefault<S[K]>
} & (O extends {other(key: string): boolean} ? {
[key: string]: unknown
} : {})

type ProxyFiggyPudding<S, O> = Readonly<Proxy<S, O>> & FiggyPudding<S, O>

type PuddingFactory<S, O> = (...providers: any[]) => ProxyFiggyPudding<S, O>

interface FiggyPuddingConstructor {
new <S extends Specs, O extends Options>(
specs: S, opts: O, providers: any[]
): FiggyPudding<S, O>
}

interface FiggyPudding<S, O> {
readonly __isFiggyPudding: true
readonly [Symbol.toStringTag]: 'FiggyPudding'

get<K extends AvailableKeys<S, O>>(key: K): K extends keyof S ? SpecDefault<S[K]> : unknown
concat<P extends any[]>(...providers: P): ProxyFiggyPudding<S, O>
toJSON(): {
[K in AvailableKeys<S, O>]: K extends keyof S ? SpecDefault<S[K]> : unknown
}
forEach<This = this>(
fn: (this: This, value: unknown, key: AvailableKeys<S, O>, opts: this) => void,
thisArg?: This
): void
entries(matcher: (key: string) => boolean): IterableIterator<[string, unknown]>
entries(): IterableIterator<[AvailableKeys<S, O>, unknown]>
[Symbol.iterator](): IterableIterator<[AvailableKeys<S, O>, unknown]>
keys(): IterableIterator<AvailableKeys<S, O>>
values(): IterableIterator<unknown>
}
}

export = figgyPudding
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "3.5.1",
"description": "Delicious, festive, cascading config/opts definitions",
"main": "index.js",
"types": "index.d.ts",
"files": [
"*.js",
"lib"
Expand Down
5 changes: 5 additions & 0 deletions test/iteration.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ test('forEach', t => {
[1, 'a', opts],
[2, 'b', opts]
], 'correct arguments, and only declared props, in declared order')

opts.forEach(function () {
thisArg = this
})
t.equal(thisArg, opts, 'correct default this')
t.done()
})

Expand Down
6 changes: 6 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"target": "es2015",
"strict": true
}
}