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 support for optional properties to pick, omit and get #2161

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
52 changes: 52 additions & 0 deletions .changeset/late-socks-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
"effect": minor
---

add support for optional property keys to `pick`, `omit` and `get`

Before:

```ts
import { pipe } from "effect/Function";
import * as S from "effect/Struct";

const struct: {
a?: string;
b: number;
c: boolean;
} = { b: 1, c: true };

// error
const x = pipe(struct, S.pick("a", "b"));

const record: Record<string, number> = {};

const y = pipe(record, S.pick("a", "b"));
console.log(y); // => { a: undefined, b: undefined }

// error
console.log(pipe(struct, S.get("a")));
```

Now

```ts
import { pipe } from "effect/Function";
import * as S from "effect/Struct";

const struct: {
a?: string;
b: number;
c: boolean;
} = { b: 1, c: true };

const x = pipe(struct, S.pick("a", "b"));
console.log(x); // => { b: 1 }

const record: Record<string, number> = {};

const y = pipe(record, S.pick("a", "b"));
console.log(y); // => {}

console.log(pipe(struct, S.get("a"))); // => undefined
```
33 changes: 25 additions & 8 deletions packages/effect/dtslint/Struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ declare const templateLiteralNumberRecord: Record<`a${string}`, number>
const stringStruct = { a: "a", b: 1, c: true }
const symbolStruct = { [asym]: "a", [bsym]: 1, [csym]: true }
const numberStruct = { 1: "a", 2: 1, 3: true }
declare const optionalStringStruct: {
a?: string
b: number
c: boolean
}

// -------------------------------------------------------------------------------------
// evolve
Expand All @@ -29,7 +34,7 @@ pipe({ a: "a", b: 2 }, S.evolve({ a: (s) => s.length }))
// get
// -------------------------------------------------------------------------------------

// @ts-expect-error
// $ExpectType unknown
pipe({}, S.get("a"))

// $ExpectType string
Expand All @@ -41,7 +46,7 @@ S.get("a")(stringStruct)
// $ExpectType number | undefined
pipe(stringNumberRecord, S.get("a"))

// $ExpectType <S extends Record<"a", any>>(s: S) => MatchRecord<S, S["a"] | undefined, S["a"]>
// $ExpectType <S extends { a?: any; }>(s: S) => MatchRecord<S, S["a"] | undefined, S["a"]>
S.get("a")

// $ExpectType string
Expand All @@ -53,7 +58,7 @@ S.get(asym)(symbolStruct)
// $ExpectType number | undefined
pipe(symbolNumberRecord, S.get(asym))

// $ExpectType <S extends Record<typeof asym, any>>(s: S) => MatchRecord<S, S[typeof asym] | undefined, S[typeof asym]>
// $ExpectType <S extends { [asym]?: any; }>(s: S) => MatchRecord<S, S[typeof asym] | undefined, S[typeof asym]>
S.get(asym)

// $ExpectType string
Expand All @@ -65,7 +70,7 @@ S.get(1)(numberStruct)
// $ExpectType number | undefined
pipe(numberNumberRecord, S.get(1))

// $ExpectType <S extends Record<1, any>>(s: S) => MatchRecord<S, S[1] | undefined, S[1]>
// $ExpectType <S extends { 1?: any; }>(s: S) => MatchRecord<S, S[1] | undefined, S[1]>
S.get(1)

// $ExpectType number | undefined
Expand All @@ -77,6 +82,12 @@ pipe(hole<Record<string, number> & { a: boolean }>(), S.get("a"))
// @ts-expect-error
pipe(hole<Record<string, number> & { a: boolean }>(), S.get("b"))

// $ExpectType string | undefined
pipe(optionalStringStruct, S.get("a"))

// $ExpectType string | undefined
S.get("a")(optionalStringStruct)

// -------------------------------------------------------------------------------------
// pick
// -------------------------------------------------------------------------------------
Expand All @@ -93,7 +104,7 @@ S.pick("d" as string)(stringStruct)
// $ExpectType { a: string; b: number; }
pipe(stringStruct, S.pick("a", "b"))

// $ExpectType { a: number | undefined; b: number | undefined; }
// $ExpectType { a?: number; b?: number; }
pipe(stringNumberRecord, S.pick("a", "b"))

// @ts-expect-error
Expand All @@ -108,7 +119,7 @@ S.pick(dsym as symbol)(symbolStruct)
// $ExpectType { [asym]: string; [bsym]: number; }
pipe(symbolStruct, S.pick(asym, bsym))

// $ExpectType { [asym]: number | undefined; [bsym]: number | undefined; }
// $ExpectType { [asym]?: number; [bsym]?: number; }
pipe(symbolNumberRecord, S.pick(asym, bsym))

// $ExpectType { 1: string; 2: number; }
Expand All @@ -123,10 +134,10 @@ S.pick(4)(numberStruct)
// $ExpectType { [x: number]: unknown; }
S.pick(4 as number)(numberStruct)

// $ExpectType { 1: number | undefined; 2: number | undefined; }
// $ExpectType { 1?: number; 2?: number; }
pipe(numberNumberRecord, S.pick(1, 2))

// $ExpectType { ab: number | undefined; aa: number | undefined; }
// $ExpectType { ab?: number; aa?: number; }
pipe(templateLiteralNumberRecord, S.pick("aa", "ab"))

// $ExpectType { a: boolean; }
Expand All @@ -135,6 +146,9 @@ pipe(hole<Record<string, number> & { a: boolean }>(), S.pick("a"))
// @ts-expect-error
pipe(hole<Record<string, number> & { a: boolean }>(), S.pick("b"))

// $ExpectType { a?: string; b: number; }
pipe(optionalStringStruct, S.pick("a", "b"))

// -------------------------------------------------------------------------------------
// omit
// -------------------------------------------------------------------------------------
Expand Down Expand Up @@ -174,3 +188,6 @@ pipe(symbolNumberRecord, S.omit(asym))

// $ExpectType { [x: number]: number; }
pipe(numberNumberRecord, S.omit(1))

// $ExpectType { a?: string; b: number; }
pipe(optionalStringStruct, S.omit("c"))
13 changes: 8 additions & 5 deletions packages/effect/src/Struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ import type { MatchRecord, Simplify } from "./Types.js"
export const pick = <Keys extends Array<PropertyKey>>(
...keys: Keys
) =>
<S extends Record<Keys[number], any>>(
<S extends { [K in Keys[number]]?: any }>(
s: S
): MatchRecord<S, { [K in Keys[number]]: S[K] | undefined }, { [K in Keys[number]]: S[K] }> => {
): MatchRecord<S, { [K in Keys[number]]?: S[K] }, Simplify<Pick<S, Keys[number]>>> => {
const out: any = {}
for (const k of keys) {
out[k] = (s as any)[k]
if (k in s) {
out[k] = (s as any)[k]
}
}
return out
}
Expand All @@ -47,7 +49,7 @@ export const pick = <Keys extends Array<PropertyKey>>(
export const omit = <Keys extends Array<PropertyKey>>(
...keys: Keys
) =>
<S extends Record<Keys[number], any>>(s: S): Simplify<Omit<S, Keys[number]>> => {
<S extends { [K in Keys[number]]?: any }>(s: S): Simplify<Omit<S, Keys[number]>> => {
const out: any = { ...s }
for (const k of keys) {
delete out[k]
Expand Down Expand Up @@ -167,4 +169,5 @@ export const evolve: {
* @since 2.0.0
*/
export const get =
<K extends PropertyKey>(key: K) => <S extends Record<K, any>>(s: S): MatchRecord<S, S[K] | undefined, S[K]> => s[key]
<K extends PropertyKey>(key: K) => <S extends { [P in K]?: any }>(s: S): MatchRecord<S, S[K] | undefined, S[K]> =>
s[key]
73 changes: 70 additions & 3 deletions packages/effect/test/Struct.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,79 @@ import { assert, describe, expect, it } from "vitest"

describe("Struct", () => {
it("exports", () => {
expect(Struct.getOrder).exist // alias of order.struct, tested there
expect(Struct.getOrder).exist // alias of `Order.ts#struct`
})

it("pick", () => {
expect(pipe({ a: "a", b: 1, c: true }, Struct.pick("a", "b"))).toEqual({ a: "a", b: 1 })
const record: Record<string, number> = {}
expect(pipe(record, Struct.pick("a", "b"))).toStrictEqual({ a: undefined, b: undefined })

const record1: Record<string, number> = {}
expect(pipe(record1, Struct.pick("a", "b"))).toStrictEqual({})
const record2: Record<string, number> = { b: 1 }
expect(pipe(record2, Struct.pick("a", "b"))).toStrictEqual({ b: 1 })

const optionalStringStruct1: {
a?: string
b: number
c: boolean
} = { b: 1, c: true }
expect(pipe(optionalStringStruct1, Struct.pick("a", "b"))).toStrictEqual({ b: 1 })
const optionalStringStruct2: {
a?: string
b: number
c: boolean
} = { a: "a", b: 1, c: true }
expect(pipe(optionalStringStruct2, Struct.pick("a", "b"))).toStrictEqual({ a: "a", b: 1 })

const a = Symbol.for("a")
const optionalSymbolStruct1: {
[a]?: string
b: number
c: boolean
} = { b: 1, c: true }
expect(pipe(optionalSymbolStruct1, Struct.pick(a, "b"))).toStrictEqual({ b: 1 })
const optionalSymbolStruct2: {
[a]?: string
b: number
c: boolean
} = { [a]: "a", b: 1, c: true }
expect(pipe(optionalSymbolStruct2, Struct.pick(a, "b"))).toStrictEqual({ [a]: "a", b: 1 })
})

it("omit", () => {
expect(pipe({ a: "a", b: 1, c: true }, Struct.omit("c"))).toEqual({ a: "a", b: 1 })

const record1: Record<string, number> = {}
expect(pipe(record1, Struct.omit("a", "c"))).toStrictEqual({})
const record2: Record<string, number> = { b: 1 }
expect(pipe(record2, Struct.omit("a", "c"))).toStrictEqual({ b: 1 })

const optionalStringStruct1: {
a?: string
b: number
c: boolean
} = { b: 1, c: true }
expect(pipe(optionalStringStruct1, Struct.omit("c"))).toStrictEqual({ b: 1 })
const optionalStringStruct2: {
a?: string
b: number
c: boolean
} = { a: "a", b: 1, c: true }
expect(pipe(optionalStringStruct2, Struct.omit("c"))).toStrictEqual({ a: "a", b: 1 })

const a = Symbol.for("a")
const optionalSymbolStruct1: {
[a]?: string
b: number
c: boolean
} = { b: 1, c: true }
expect(pipe(optionalSymbolStruct1, Struct.omit("c"))).toStrictEqual({ b: 1 })
const optionalSymbolStruct2: {
[a]?: string
b: number
c: boolean
} = { [a]: "a", b: 1, c: true }
expect(pipe(optionalSymbolStruct2, Struct.omit("c"))).toStrictEqual({ [a]: "a", b: 1 })
})

it("evolve", () => {
Expand Down Expand Up @@ -57,4 +119,9 @@ describe("Struct", () => {
false
)
})

it("get", () => {
expect(pipe({ a: 1 }, Struct.get("a"))).toStrictEqual(1)
expect(pipe({}, Struct.get("a"))).toStrictEqual(undefined)
})
})
Loading