Typescript Can't implement generic function interface

AdMer

I'm trying to implement a generic function interface and I cannot make it work.

IToken.ts

export interface IToken {
  token: string;
  expires: number;
}

ITokenMapper.ts

export interface ITokenMapper {
  <T>(apiResult: any): T;
}

tokenMapper.ts

import {ITokenMapper} from "./interfaces/ITokenMapper";
import {IToken} from "./interfaces/IToken";

export const tokenMapper: ITokenMapper = function <IToken>(apiResult: any): IToken {
  if(apiResult.token && apiResult.expires) {
    return {token: apiResult.token as string, expires: apiResult.expires as number}
  }
  throw new Error('Unable to parse token');
};

Here is a screenshot from tokenMapper.ts saying IToken import is unused but I should have a use for it:

tokenMapper.ts

Edit : Using Typescript 3.0.3

Joe

I believe you can accomplish your typing with a generic interface ITokenMapper<T>

interface IToken {
    token: string;
    expires: number;
}

interface ITokenMapper<T> {
    (apiResult: T): T;
}

const tokenMapper: ITokenMapper<IToken> = function (apiResult) {
    if(apiResult.token && apiResult.expires) {
      return { token: apiResult.token as string, expires: apiResult.expires as number};
    }

    throw new Error('Unable to parse token');
};

From: https://www.typescriptlang.org/docs/handbook/generics.html#generic-types

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why can't a Java Generic implement an Interface?

From Dev

Implement Interface Generic Function

From Dev

Implement Interface Generic Function

From Dev

Typescript generic that generates an interface of builder function types

From Dev

implement generic generic interface in scala

From Dev

Implement a generic interface

From Dev

Delphi interface generic function returning TobjectList<T>

From Dev

How can I implement a generic interface in Rhino JS?

From Dev

Can't create generic for an interface implementor

From Dev

Can't create generic for an interface implementor

From Dev

Entity Framework T4: POCOs implement generic interface?

From Dev

limit T in a generic class to types that implement some interface

From Dev

Entity Framework T4: POCOs implement generic interface?

From Dev

How to implement a generic interface with a child generic interface

From Dev

How to implement a generic interface with a child generic interface

From Dev

Can't add a concrete instance of a generic interface to a generic collection

From Dev

Can't add a concrete instance of a generic interface to a generic collection

From Dev

extending interface with generic in typescript

From Dev

How can I define a Typescript interface for a function?

From Dev

can a typescript call implement an interface, and make the vars private?

From Dev

Typescript compiles even though I don't implement interface

From Dev

Dictionary of classes that implement a generic interface

From Dev

How to implement an interface with generic types?

From Dev

How implement an interface Generic Method

From Dev

Dictionary of classes that implement a generic interface

From Dev

How to implement an interface with generic types?

From Dev

How implement an interface Generic Method

From Dev

TypeScript can't manage to use dictionary interface

From Dev

Generic function as functional interface?

Related Related

  1. 1

    Why can't a Java Generic implement an Interface?

  2. 2

    Implement Interface Generic Function

  3. 3

    Implement Interface Generic Function

  4. 4

    Typescript generic that generates an interface of builder function types

  5. 5

    implement generic generic interface in scala

  6. 6

    Implement a generic interface

  7. 7

    Delphi interface generic function returning TobjectList<T>

  8. 8

    How can I implement a generic interface in Rhino JS?

  9. 9

    Can't create generic for an interface implementor

  10. 10

    Can't create generic for an interface implementor

  11. 11

    Entity Framework T4: POCOs implement generic interface?

  12. 12

    limit T in a generic class to types that implement some interface

  13. 13

    Entity Framework T4: POCOs implement generic interface?

  14. 14

    How to implement a generic interface with a child generic interface

  15. 15

    How to implement a generic interface with a child generic interface

  16. 16

    Can't add a concrete instance of a generic interface to a generic collection

  17. 17

    Can't add a concrete instance of a generic interface to a generic collection

  18. 18

    extending interface with generic in typescript

  19. 19

    How can I define a Typescript interface for a function?

  20. 20

    can a typescript call implement an interface, and make the vars private?

  21. 21

    Typescript compiles even though I don't implement interface

  22. 22

    Dictionary of classes that implement a generic interface

  23. 23

    How to implement an interface with generic types?

  24. 24

    How implement an interface Generic Method

  25. 25

    Dictionary of classes that implement a generic interface

  26. 26

    How to implement an interface with generic types?

  27. 27

    How implement an interface Generic Method

  28. 28

    TypeScript can't manage to use dictionary interface

  29. 29

    Generic function as functional interface?

HotTag

Archive