Skip to content

Commit

Permalink
Implement Struct.keys as a typed alternative to Object.keys (#3282)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikearnaldi authored and gcanti committed Jul 23, 2024
1 parent 53af22a commit 8da7f7f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .changeset/lovely-buckets-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
"effect": minor
---

Implement Struct.keys as a typed alternative to Object.keys

```ts
import { Struct } from "effect"

const symbol: unique symbol = Symbol()

const value = {
a: 1,
b: 2,
[symbol]: 3
}

const keys: Array<"a" | "b"> = Struct.keys(value)
```
22 changes: 22 additions & 0 deletions packages/effect/src/Struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,25 @@ export const evolve: {
export const get =
<K extends PropertyKey>(key: K) => <S extends { [P in K]?: any }>(s: S): MatchRecord<S, S[K] | undefined, S[K]> =>
s[key]

/**
* Retrieves the object keys that are strings in a typed manner
*
* @example
* import { Struct } from "effect"
*
* const symbol: unique symbol = Symbol()
*
* const value = {
* a: 1,
* b: 2,
* [symbol]: 3
* }
*
* const keys: Array<"a" | "b"> = Struct.keys(value)
*
* assert.deepStrictEqual(keys, ["a", "b"])
*
* @since 3.6.0
*/
export const keys = <T extends {}>(o: T): Array<(keyof T) & string> => Object.keys(o) as Array<(keyof T) & string>

0 comments on commit 8da7f7f

Please sign in to comment.