20

I am trying to use a Typescript Discriminated Union to model a rather common scenario when loading data asynchronously:

type LoadingState = { isLoading: true; }
type SuccessState = { isLoading: false; isSuccess: true; }
type ErrorState =   { isLoading: false; isSuccess: false; errorMessage: string; }

type State = LoadingState | SuccessState | ErrorState;

According to my understanding, this should limit the allowed combinations of values according to the type definitions. However, the type system is happy to accept the following combination:

const testState: State = {
    isLoading: true,
    isSuccess: true,
    errorMessage: "Error!"
}

I expect an error here. Is there something I am missing or in some way misusing the type definitions?

Yakimych
  • 17,612
  • 7
  • 52
  • 69
  • Do you have the `suppressExcessPropertyErrors` compiler option enabled? – cartant Oct 06 '18 at 10:39
  • @cartant - just tried setting it to both true and false, but it didn't make any difference. – Yakimych Oct 06 '18 at 10:47
  • (This has been an open issue in TypeScript since 2017, see further discussion here: https://github.com/microsoft/TypeScript/issues/20863) – Alex Ryan May 29 '19 at 22:32
  • Seems the problem is fixed in v3.5: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-5.html#improved-excess-property-checks-in-union-types – abuduba Aug 05 '19 at 12:21

1 Answers1

39

This is an issue with the way excess property checks work on unions. If an object literal is assigned to a variable of union type, a property will not be marked as excess if it is present on any of the union members. If we don't consider excess properties to be an error (and except for object literals they are not considered an error), the object literal you specified could be an instance of LoadingState (an instance with isLoading set to true as mandated and a couple of excess properties).

To get around this undesired behavior we can add properties to LoadingState to make your object literal incompatible with LoadingState

type LoadingState = { isLoading: true; isSuccess?: undefined }
type SuccessState = { isLoading: false; isSuccess: true; }
type ErrorState =   { isLoading: false; isSuccess: false; errorMessage: string; }

type State = LoadingState | SuccessState | ErrorState;

const testState: State = { // error
    isLoading: true,
    isSuccess: true,
    errorMessage: "Error!"
}

We could even create a type that would ensure such member will be added

type LoadingState = { isLoading: true; }
type SuccessState = { isLoading: false; isSuccess: true; }
type ErrorState =   { isLoading: false; isSuccess: false; errorMessage: string; }

type UnionKeys<T> = T extends T ? keyof T : never;
type StrictUnionHelper<T, TAll> = T extends any ? T & Partial<Record<Exclude<UnionKeys<TAll>, keyof T>, undefined>> : never;
type StrictUnion<T> = StrictUnionHelper<T, T>

type State = StrictUnion< LoadingState | SuccessState | ErrorState>

const testState: State = { // error
    isLoading: true,
    isSuccess: true,
    errorMessage: "Error!"
}
Titian Cernicova-Dragomir
  • 230,986
  • 31
  • 415
  • 357
  • 1
    This is great. Pity that it isn't published in a package anywhere. I've done a request over at https://github.com/andnp/SimplyTyped/issues/76#issue-396291343 for it – balupton Jan 06 '19 at 20:33
  • It seems like it stopped working in TypeScript 3.4 though. There is no error anymore. – Karol Majewski Mar 24 '19 at 19:03
  • 1
    @KarolMajewski thanks for bringing it to my attention, will look into it... – Titian Cernicova-Dragomir Mar 24 '19 at 19:17
  • 1
    @KarolMajewski This works with the current of RC of 3.4, but I plan to report an issue .. this seems to be a breaking change in the way type aliases are resolved: `type StrictUnionHelper = T extends any ? T & Partial, never>> : never; type StrictUnion = StrictUnionHelper>` – Titian Cernicova-Dragomir Mar 24 '19 at 19:31
  • 1
    @TitianCernicova-Dragomir StrictUnion really needs to be an officially supported concept. It's a nightmare dealing with unions if you deliberately have disjunct types. – Simon_Weaver Jul 21 '20 at 04:58
  • Is there by any chance a ready-to-use implementation of `DeepStrictUnion` somewhere? I'm trying to merge overlapping type definitions for AST from different JS parsers into something usable. Such a PITA... – thorn0 Jan 14 '21 at 14:52