pfgray / ts-adt

Generate Algebraic Data Types and pattern matchers
MIT License
316 stars 13 forks source link

Provide a Type to infer adt member #21

Closed val-o closed 3 years ago

val-o commented 3 years ago

Hi people, I couldn't find something to infer union member.

Can we make some helper for it so we could use it like this

type Left = MemberOf<Either, 'left'>
anthonyjoeseph commented 3 years ago

Would MakeAdtMember work?

import { MakeADTMember } from 'ts-adt/MakeADT';

type Either = { _type: 'left'; value1: number } | { _type: 'Right'; value2: string }

type leftside = MakeADTMember<'_type', Either, 'left'> // leftside = { _type: 'Left'; value1: number; }
type leftval = leftside['value1'] // leftval = number
val-o commented 3 years ago

Thanks, thats exatly what I need