Skip to content

Commit

Permalink
fix(f.narrow): variance
Browse files Browse the repository at this point in the history
  • Loading branch information
millsp committed Mar 2, 2021
1 parent ec4a953 commit 866ecd7
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions sources/Function/Narrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ import {Cast} from '../Any/Cast'
import {Narrowable} from './_Internal'

/**
* Basic type helper
* @hidden
*/
type _Narrow<A> =
type NarrowRaw<A> =
| []
| (A extends Narrowable ? A : never)
| ({[K in keyof A]: _Narrow<A[K]>})
| ({[K in keyof A]: NarrowRaw<A[K]>})

/**
* Enforce proper variance
* @hidden
*/
type NarrowVar<A, N = NarrowRaw<A>> =
N | Cast<A, N>

/**
* Prevent type widening on generic function parameters
Expand All @@ -28,6 +36,6 @@ type _Narrow<A> =
* ```
*/
type Narrow<A extends any> =
Cast<A, _Narrow<A>>
NarrowVar<A>

export {Narrow}

0 comments on commit 866ecd7

Please sign in to comment.