14

How to express an interface (IResponse), with one property has a string key (which is not known statically). Below, the key values can be anything like books, chairs, etc. All other keys and types are known statically. Below implementation gives error. I guess the error is because the index signature on the IResponse makes all the property values to be IValue[]. Is there a way to do this?

export interface IMeta{}
export interface IValue{}
export interface IResponse {
     meta: IMeta;
     [index: string]:IValue[];
}

 export class Response implements IResponse {
    meta:IMeta;
    values:IValue[];
    //books:IValue[];
    //anything:IValue[];
 }
2
  • Not that I know of. However, since the properties can be named anything and are defined on the class anyway, do they really need to be in an interface to begin with? May 28, 2014 at 14:39
  • thanks. without the definition, I may not be able to access the property like d["books"], with implicit any set. It will give TS7017 "Index signature of object type implicitly has an 'any' type. The interface define input from an external source, which is of format jsonapi.org/format
    – bsr
    May 28, 2014 at 15:34

4 Answers 4

38

Old question, but here is how I solved the issue for myself.

export interface Foo {
    [key: string]: any;
}

{ something: 0 } as Foo => valid

{ other: 'hello' } as Foo => valid

Edit: January 14, 2023: There is a better way to do this for type safety if you know what the possible shapes are:

type ApiResponse<Type> = {
  [Property in keyof Type]: Type[Property];
};

type BookResponse = { title: string; }

const myBookResponse: ApiResponse<BookResponse> = { title: 'foo' } // OK
const myBookResponse: ApiResponse<BookResponse> = { title: 42 } // ERROR
const myBookResponse: ApiResponse<BookResponse> = { title: 'foo', taco: true } // ERROR


2
  • 3
    This should be the selected answer. Thank you for sharing!
    – tmuecksch
    Dec 31, 2018 at 8:01
  • This amounts to turning off typechecking completely except for checking that key is a string. It is not a solution at all.
    – Alex Bausk
    Jul 21, 2021 at 16:00
12

If you define one interface for the known types and a separate one for the "unknown" types, you could use a type assertion to tell the compiler the one you wanted to use.

It isn't ideal, but you are working in a edge case (i.e. not entirely dynamic, not entirely static).

export interface IMeta{}
export interface IValue{}
export interface IFunkyResponse {
     [index: string]:IValue[];
}
export interface IResponse {
     meta: IMeta;
}

export class Response implements IResponse {
    meta:IMeta;
    values:IValue[];
    books:IValue[];
    anything:IValue[];
}

You can type-assert between <IResponse> resp and <IFunkyResponse> resp to access one or the other of the styles.

2
  • Did you mean something like goo.gl/WSoodm . Can you please show how to typecast.
    – bsr
    May 28, 2014 at 19:56
  • That's the idea - you won't like the type assertion, but bear in mind this isn't your typical case... var tmp = (<IFunkyResponse><any>d);
    – Fenton
    May 29, 2014 at 7:11
2

Without interface:

const myFunc = ((myObjectWithSomeKeys: { [key: string]: any }) => {
});
-2

There seem to be no exact replies unfortunately, only approximations of a solution.

The solution is to use intersection type over two interfaces, one that defines static metadata and one that defines dynamic keys:

interface IStatic {
  staticflag: boolean;
}

interface IDynamic {
  [x: string]: string | number;
}

type Complex = IStatic & IDynamic;

const a: Complex = {
  staticflag: false,
  dynamic1: 'a',
};
1

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.