Skip to content

Generic Type Alias #1959

Closed
Closed
@RamIdeas

Description

@RamIdeas

What is the reason for disallowing generic type aliases?

I know you can use an empty interface definition to achieve the same thing, such as:

interface KO<T> extends KnockoutObservable<T> {}

But the limitation with interfaces is you can't use primitives or union types.

I want to be able to write:

type KnockoutWrapped<T> = T | KnockoutObservable<T>;
// or: type KW<T> = T | KO<T>;
// ensure I call ko.unwrap to get value

But my only option right now is:

type KnockoutWrappedString = string | KnockoutObservable<string>;
type KnockoutWrappedNumber = number | KnockoutObservable<number>;
type KnockoutWrappedBoolean = boolean | KnockoutObservable<boolean>;
// ...
// or:
type KWString = string | KO<string>;
type KWNumber = number | KO<number>;
type KWBoolean = boolean | KO<boolean>;

Which:

  1. reduces my flexibility to use whatever type I want (without having to create more interfaces)
  2. reduces readability as Visual Studio's colourisation won't colour string, number and boolean in blue, and
  3. quite frankly, makes me type more -- reminiscent of back before typescript included generics and I was writing:
interface KOString extends KnockoutObservableString {}
interface KONumber extends KnockoutObservableNumber {}
interface KOBoolean extends KnockoutObservableBoolean {}
// ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions