discriminated-union

Collapsing a discriminated union - derive an umbrella type with all possible key-value combinations from the union

北城以北 提交于 2021-02-20 19:23:52
问题 I have a discriminated union, for example: type Union = { a: "foo", b: string, c: number } | {a: "bar", b: boolean } I need to derive a type that includes all potential properties, assigned with types that may be found on any member of Union , even if only defined on some - in my example: type CollapsedUnion = { a: "foo" | "bar", b: string | boolean, c: number | undefined } How can I make a generic that derives such collapsed unions? I need a generic that supports unions of any size. Similar

Collapsing a discriminated union - derive an umbrella type with all possible key-value combinations from the union

﹥>﹥吖頭↗ 提交于 2021-02-20 19:23:07
问题 I have a discriminated union, for example: type Union = { a: "foo", b: string, c: number } | {a: "bar", b: boolean } I need to derive a type that includes all potential properties, assigned with types that may be found on any member of Union , even if only defined on some - in my example: type CollapsedUnion = { a: "foo" | "bar", b: string | boolean, c: number | undefined } How can I make a generic that derives such collapsed unions? I need a generic that supports unions of any size. Similar

Can TypeScript infer type of a discriminated union via “extracted” boolean logic?

大兔子大兔子 提交于 2021-02-05 08:17:05
问题 I have been using discriminated unions (DU) more frequently and have come to love them. I do however have one issue I can't seem to get past. If you inline a boolean check for the DU, you can rely on TypeScript (TS) to automatically infer the type for you. However, if you extract the boolean check, TS can no longer narrow to the specific subtype of the DU. I’m aware of type guards, but I’d like to know why the compiler doesn’t support extracted online checks, specifically. Is this a known

Can I define a Typescript map having a value constraint corresponding with each value's key?

孤人 提交于 2021-01-29 17:21:34
问题 In this playground I would like to create a map containing at most a single action of each type in the Action union. Each of the unioned types is differentiated by having a different string literal property 'type'. I have a definition for this map which compiles but is too loose... const lastAction:{ [A in Action["type"]]?:Action } = {} The constraint on keys within the lastAction map enforces that... the key is a "type" property from some Action type the value must be some Action type ...it

Adding constant fields to F# discriminated unions

家住魔仙堡 提交于 2021-01-26 10:32:27
问题 Is it possible to add constant field values to F# discriminated unions? Can I do something like this? type Suit | Clubs("C") | Diamonds("D") | Hearts("H") | Spades("S") with override this.ToString() = // print out the letter associated with the specific item end If I were writing a Java enum, I would add a private value to the constructor like so: public enum Suit { CLUBS("C"), DIAMONDS("D"), HEARTS("H"), SPADES("S"); private final String symbol; Suit(final String symbol) { this.symbol =

Unable to cast string to type with implicit conversion

早过忘川 提交于 2021-01-07 06:38:35
问题 I am trying to serialize JSON into a data structure with the types from the OneOf library, using JSON.NET, using a custom converter. I am encountering the following exception: System.InvalidCastException: 'Unable to cast object of type 'System.String' to type 'System.Nullable`1[OneOf.OneOf`2[OneOf.OneOf`2[PandocFilters.TagContent,System.Int64][],System.String]]'.' Which doesn't make sense to me, because the following C# code compiles and runs: OneOf<OneOf<TagContent, long>[], string>? c =

Unable to cast string to type with implicit conversion

北战南征 提交于 2021-01-07 06:36:13
问题 I am trying to serialize JSON into a data structure with the types from the OneOf library, using JSON.NET, using a custom converter. I am encountering the following exception: System.InvalidCastException: 'Unable to cast object of type 'System.String' to type 'System.Nullable`1[OneOf.OneOf`2[OneOf.OneOf`2[PandocFilters.TagContent,System.Int64][],System.String]]'.' Which doesn't make sense to me, because the following C# code compiles and runs: OneOf<OneOf<TagContent, long>[], string>? c =

TypeScript: Map union type to another union type

筅森魡賤 提交于 2020-07-18 10:39:13
问题 Is it possible to map a union type to another union type in TypeScript? What I'd Like to be able to do e.g. Given a union type A: type A = 'one' | 'two' | 'three'; I'd like to be able to map it to union type B: type B = { type: 'one' } | { type: 'two'} | { type: 'three' }; What I have tried type B = { type: A }; But this results in: type B = { type: 'one' | 'two' | 'three' }; which is not quite what I want. 回答1: You can use conditional type for distributing over the members of the union type

Discriminated Union of Generic type

吃可爱长大的小学妹 提交于 2020-07-15 02:43:10
问题 I'd like to be able to use union discrimination with a generic. However, it doesn't seem to be working: Example Code (view on typescript playground): interface Foo{ type: 'foo'; fooProp: string } interface Bar{ type: 'bar' barProp: number } interface GenericThing<T> { item: T; } let func = (genericThing: GenericThing<Foo | Bar>) => { if (genericThing.item.type === 'foo') { genericThing.item.fooProp; // this works, but type of genericThing is still GenericThing<Foo | Bar> let fooThing =

IgnoreMissingMember setting doesn't seem to work with FSharpLu.Json deserializer

两盒软妹~` 提交于 2020-07-03 09:59:11
问题 This is a following to: deserialization issue, with json.net, in F#. I am deserializing some JSON that has an extra, unbound property using FSharpLu.Json. Here is the code: open System open Newtonsoft.Json open Microsoft.FSharpLu.Json type r = { a: int } let a = "{\"a\":3, \"b\":5}" Compact.TupleAsArraySettings.settings.MissingMemberHandling <- MissingMemberHandling.Ignore Compact.deserialize<r> a // doesn't work Despite setting MissingMemberHandling.Ignore it returns a json.net error: Could