Skip to content

OneOf access by case and inference #59146

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

Closed
6 tasks done
maxime1992 opened this issue Jul 5, 2024 · 5 comments
Closed
6 tasks done

OneOf access by case and inference #59146

maxime1992 opened this issue Jul 5, 2024 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@maxime1992
Copy link

maxime1992 commented Jul 5, 2024

πŸ” Search Terms

  • Generic derived value type
  • oneOf

βœ… Viability Checklist

⭐ Suggestion

Here's a very simple example:

interface CaseA {
  valueA: any;
}

interface CaseB {
  valueB: any;
}

type OneOf = {
    $case: "caseA";
    caseA: CaseA;
} | {
    $case: "caseB";
    caseB: CaseB;
} 

declare const request: OneOf

// I know that without further checks, on Typescript side, on the OneOf type, we're only sure that
// `$case` exists and we're unsure about the rest. Yet, as human looking at this type, I know we'll
// have either the caseA or caseB and therefore accessing the OneOf type by its $case should result
// in a type of `CaseA | CaseB`. Is there any way of making this work?
request[request.$case]

Here's a link to a Typescript Playground

πŸ“ƒ Motivating Example

When using a oneOf with a discriminator, you can now access directly the value by doing yourOneOfVariable[yourOneOfVariable.discriminator] and it'll give you the union of the values.

πŸ’» Use Cases

  1. What do you want to use this for?

We have a lot of oneOf equivalent in our codebase, especially as we're using protobuf and this is a very classic usecase.

  1. What shortcomings exist with current approaches?

Not even sure this can be achieve in a type safe way currently?

  1. What workarounds are you using in the meantime?

Casting the variable as any and then using as again to cast to the union type.

@Jamesernator
Copy link

This is dependent typing which TypeScript doesn't support.

(There is a draft for a weak form of dependent typing, but I don't think is strong enough for the behaviour wanted here)

@jcready
Copy link

jcready commented Jul 5, 2024

The better way to model the one of is like this:

type OneOf = {
    $case: "caseA";
    $value: CaseA;
} | {
    $case: "caseB";
    $value: CaseB;
} 

Now you can just access request.$value and the type will be CaseA | CaseB and you can refine the type by branching off the request.$case.

@maxime1992
Copy link
Author

Thanks @Jamesernator it always hard to find the correct name of the feature one is looking for, but TIL about depending typing :) !

@jcready it is indeed except that this bit is not done on our side it's generated by a 3rd party lib. I'd need to see if we can tweak it somehow through configuration.

Hopefully dependant typing lands one day though 🀞

@jcalz
Copy link
Contributor

jcalz commented Jul 5, 2024

Essentially the same problem as #30581 and TS's facility to deal with it is described in #47109. The example here could look like

interface MyMap {
  caseA: CaseA;
  caseB: CaseB
}

type OneOf<K extends keyof MyMap = keyof MyMap> =
  { [P in K]: { $case: P } & Record<P, MyMap[P]> }[K]

function getVal<K extends keyof MyMap>(request: OneOf<K>): MyMap[K] {
  const req: Record<K, MyMap[K]> = request;
  return req[request.$case]
}

declare const request: OneOf
const either = getVal(request);
//    ^? const either: CaseA | CaseB

declare const requestB: {$case: "caseB", caseB: CaseB};
const caseB = getVal(requestB);
//    ^? const caseB: CaseB

Playground link

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 9, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

6 participants