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

Support a union Shape or polymorphic types #105

Open
sam-goodwin opened this issue Feb 3, 2020 · 4 comments
Open

Support a union Shape or polymorphic types #105

sam-goodwin opened this issue Feb 3, 2020 · 4 comments
Labels
Projects

Comments

@sam-goodwin
Copy link
Owner

sam-goodwin commented Feb 3, 2020

It should be possible to define union types for things such as polymorphic DynamoDB Tables.

See: #103 for customer request.

Ex. usage:

union([string, integer, A, array(string)])
@sam-goodwin
Copy link
Owner Author

Should also support tagged unions. I wonder if we could introduce literal types for this use case:

class A extends Record({
  tag: 'A'
}) {}

class B extends Record({
  tag: 'B'
}) {}

const taggedUnion = union([A, B], 'tag');

@sam-goodwin sam-goodwin added this to Todo in v0.13.0 Feb 4, 2020
@sam-goodwin
Copy link
Owner Author

sam-goodwin commented Feb 5, 2020

Useful mechanic for extracting a type from a tagged-union by its tag.

https://gcanti.github.io/typelevel-ts/modules/index.ts.html#taggedunionmember-type-alias

(Copied from above link)

Signature

export type TaggedUnionMember<A extends object, Tag extends keyof A, Value extends A[Tag]> = Extract<
  A,
  Record<Tag, Value>
>

Example

import { TaggedUnionMember } from 'typelevel-ts'

type A = { tag: 'A'; a: string }
type B = { tag: 'B'; b: number }
type C = A | B
export type Result = TaggedUnionMember<C, 'tag', 'A'> // A

@sam-goodwin
Copy link
Owner Author

As per #108:

I just realized that the pattern: class X extends <function call> is the "Shape equivalent" of TS's type X = <type computation>. This will come in handy for expressing all sorts of type computations, like unions:

// in the TypeScript type-system
type A = string | number;
// in the Shape type-system
class A extends Union([string, number]) {}

@sam-goodwin
Copy link
Owner Author

sam-goodwin commented Feb 5, 2020

Pretty meta to now also have a mapped type equivalent in the data-based Shape type-system .. woah. "Shape" is turning out to be an incredibly expressive and powerful DDL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Development

No branches or pull requests

1 participant