Skip to content
This repository has been archived by the owner on May 5, 2023. It is now read-only.

Improve the developer experience for polymorphic models in the new TypeScript SDK. #2960

Closed
amarzavery opened this issue Jun 8, 2018 · 1 comment

Comments

@amarzavery
Copy link
Contributor

This issue highlights the pain our customers have to go through while using the SDK against polymorphic models.

Multiple things need to be done to solve this problem.

  1. The discriminator property should be an Enum that provides intellisense for all the possible values one can provide for.
  2. The documentation/description of the discriminator property needs to document all the possible values. This ensures a decent experience for customers using JS.
  3. We should provide create() methods for all our model interfaces. Customers can use this to create models that satisfy the interface definition. This create method will not expose the discriminator property as it knows the value that should be provided for that property.
    This is a generic solution that can be applied to all the model types.
export interface Foo {
  name: string;
  odataType: string,
  age: number
}

export namespace Foo {
  export function create(params: FooParams): Foo {
     . . . 
  }
}

Creating a separate interface FooParams with all the properties duplicated from Foo would not make sense.

export interface DiscriminatedFoo {
  odataType: string
}

export interface Foo extends DiscriminatedFoo {
  name: string;
  age: number;
}
@ghost ghost added this to the Sprint-120 milestone Jun 11, 2018
@RikkiGibson
Copy link
Member

I'd like to try using a tagged union for the parameter type and seeing if that helps the tooling experience at all.

In vscode, if you declare types like so:

interface Foo {
  kind: "foo";
  foo: number;
}

interface Bar {
  kind: "bar";
  bar: string;
}

function someFunc(arg: Foo | Bar) {
}

someFunc({
  kind: "foo", // as soon as you fill out the "kind", you get intellisense for the polymorphic properties
  foo: 42
})

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants