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

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

Merged
merged 1 commit into from
Jul 18, 2024

Conversation

mikearnaldi
Copy link
Member

@mikearnaldi mikearnaldi commented Jul 17, 2024

Allows for use cases such as:

import * as S from "@effect/schema/Schema"
import { Struct } from "effect"

export const Version = S.String.pipe(
  S.pattern(/^\d+\.\d+\.\d+$/) // Ensure "<major>.<minor>.<patch>" and no leading or trailing
)

export const Email = S.String.pipe(
  S.pattern(
    /^(?!\.)(?!.*\.\.)([A-Z0-9_+-.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9-]*\.)+[A-Z]{2,}$/i
  )
)
export const Entity = S.Struct({
  id: S.UUID,
  version: Version
})
export class Creator extends S.Class<Creator>("creator")({
  ...Entity.fields,
  createdAt: S.Date,
  communicationChannels: S.Array(S.String),
  email: Email,
  platforms: S.Array(S.String)
}) {}

////// Usecase
export const UseCaseCreateCreator = S.Struct(
  Struct.omit(Creator.fields, ...Struct.keys(Entity.fields))
)

Copy link

changeset-bot bot commented Jul 17, 2024

🦋 Changeset detected

Latest commit: 03643fe

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 31 packages
Name Type
effect Minor
@effect/cli Major
@effect/cluster-browser Major
@effect/cluster-node Major
@effect/cluster-workflow Major
@effect/cluster Major
@effect/experimental Major
@effect/opentelemetry Major
@effect/platform-browser Major
@effect/platform-bun Major
@effect/platform-node-shared Major
@effect/platform-node Major
@effect/platform Major
@effect/printer-ansi Major
@effect/printer Major
@effect/rpc-http Major
@effect/rpc Major
@effect/schema Major
@effect/sql-d1 Major
@effect/sql-drizzle Major
@effect/sql-kysely Major
@effect/sql-mssql Major
@effect/sql-mysql2 Major
@effect/sql-pg Major
@effect/sql-sqlite-bun Major
@effect/sql-sqlite-node Major
@effect/sql-sqlite-react-native Major
@effect/sql-sqlite-wasm Major
@effect/sql Major
@effect/typeclass Major
@effect/vitest Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@mikearnaldi
Copy link
Member Author

/rebase

@tim-smart
Copy link
Member

We also have Record.keys which is similar, but incorrectly removes symbols from the type.

Copy link
Member

@tim-smart tim-smart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it doesn't hurt to have it in both modules.

@tim-smart tim-smart merged commit f274753 into next-minor Jul 18, 2024
9 checks passed
@tim-smart tim-smart deleted the feat/typed-keys branch July 18, 2024 01:01
@github-actions github-actions bot mentioned this pull request Jul 18, 2024
@github-actions github-actions bot mentioned this pull request Jul 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants