Home > OS >  is it possible to declare a array object type in typescript
is it possible to declare a array object type in typescript

Time:03-15

I want to declare an array object in typescript like this:

export async function getInitialState(): Promise<{
  settings?: Partial<LayoutSettings>;
  currentUser?: API.CurrentUser;
  loading?: boolean;
  dictionary?: Dictionary[];
  fetchUserInfo?: () => Promise<API.CurrentUser | undefined>;
  fetchDictionary?: () => Promise<API.DictionaryList | undefined>;
}> {}

but the visual studio shows error:

Cannot find name 'Dictionary'.ts(2304)

I have already declared Dictionary in context like this:

declare namespace API {
  type Dictionary = {
    key,
    value,
    dict_type,
  };
}

why still tell that could not found the Dictionary? is it possible to declare an array object type in typescript?

CodePudding user response:

I am guessing it's most likely you not exporting the type properly, nothing to do with the array type. If your use-case allows it, try putting it in the same file:

type Dictionary = {
    key: string,
    value: string,
    dict_type: string,
};

export async function getInitialState(): Promise<{
  settings?: Partial<LayoutSettings>;
  currentUser?: API.CurrentUser;
  loading?: boolean;
  dictionary?: Dictionary[];
  fetchUserInfo?: () => Promise<API.CurrentUser | undefined>;
  fetchDictionary?: () => Promise<API.DictionaryList | undefined>;
}> {}
  • Related