Skip to content

Commit

Permalink
feat: add HashMap.HashMap.Entry utility type (#3618)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinassefranche authored and fubhy committed Sep 18, 2024
1 parent d790dc1 commit e8009e6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/chilled-hotels-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

Adds HashMap.HashMap.Entry type helper
10 changes: 9 additions & 1 deletion packages/effect/dtslint/HashMap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { pipe } from "effect/Function"
import { hole, pipe } from "effect/Function"
import * as HashMap from "effect/HashMap"
import * as Predicate from "effect/Predicate"
import type * as Types from "effect/Types"

declare const hmLiterals: HashMap.HashMap<"k", "v">
declare const numbers: HashMap.HashMap<string, number>
Expand All @@ -23,6 +24,13 @@ export type K = HashMap.HashMap.Key<typeof hmLiterals>
// $ExpectType "v"
export type V = HashMap.HashMap.Value<typeof hmLiterals>

// -------------------------------------------------------------------------------------
// HashMap.Entry
// -------------------------------------------------------------------------------------

// $ExpectType ["k", "v"]
hole<Types.Simplify<HashMap.HashMap.Entry<typeof hmLiterals>>>()

// -------------------------------------------------------------------------------------
// filter
// -------------------------------------------------------------------------------------
Expand Down
16 changes: 16 additions & 0 deletions packages/effect/src/HashMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ export declare namespace HashMap {
* @category type-level
*/
export type Value<T extends HashMap<any, any>> = [T] extends [HashMap<infer _K, infer _V>] ? _V : never

/**
* This type-level utility extracts the entry type `[K, V]` from a `HashMap<K, V>` type.
*
* @example
* import { HashMap } from "effect"
*
* declare const hm: HashMap.HashMap<string, number>
*
* // $ExpectType [string, number]
* type V = HashMap.HashMap.Entry<typeof hm>
*
* @since 3.9.0
* @category type-level
*/
export type Entry<T extends HashMap<any, any>> = [Key<T>, Value<T>]
}

/**
Expand Down

0 comments on commit e8009e6

Please sign in to comment.