1

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

6
  • Try to avoid using any at best. I also do not understand why you use type hinting in your tokenMapper. What do you want to achieve? Commented Sep 19, 2018 at 13:26
  • What type does your apiResult have? Commented Sep 19, 2018 at 13:28
  • @k0pernikus I'd create a generic micro application where we could parse result from api token request to a personal interface Commented Sep 19, 2018 at 13:30
  • @k0pernikus I don't know, that's to goal of my parser, filter informations from a request and keep token string and when it will expire Commented Sep 19, 2018 at 13:31
  • Why not create an Interface directly for your apiResponse? Commented Sep 19, 2018 at 13:32

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

@AdMer Please add in the comment or as an answer of your own what you did change so that it now fits your use-case, so that others with a similar problem may benefit from it as well.
@k0pernikus sure i just did it
2

So here is what I changed to make it work

ITokenMapper.ts -> IMapper.ts renamed and updated

export type IMapper<T> = (apiResult: any) => T;

tokenMapper.ts -> abpTokenMapper.ts renamed and updated

import { IMapper } from "../../utils/IMapper";
import { IToken } from "../interfaces/IToken";

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.