0

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?

2
  • 1
    Probably you should reference the type Dictionary like: Api.Dictionary since it belongs to the Api namespace. Commented Mar 14, 2022 at 17:31
  • 1
    You can definitely declare array type in typescript, array is not the problem here, you can verify that be removing the array declaration and it would still throw the same error. I'm not very sure why you need a namespace here, but did you export it? Also, within the dictionary type, you should define the type of key, value, and dict_type as well. Commented Mar 14, 2022 at 17:31

1 Answer 1

1

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>;
}> {}
Sign up to request clarification or add additional context in comments.

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.