Home > Net >  How to understand this generic interface definition in TypeScript?
How to understand this generic interface definition in TypeScript?

Time:06-15

Does the definition of this Cache type, Cache<V = any>, define an object or a function?

Here the generic V, why specify an any? What does the equals sign mean?

export interface Cache<V = any> {
  value?: V;
  timeoutId?: ReturnType<typeof setTimeout>;
  time?: number;
  alive?: number;
}

CodePudding user response:

Just do not write declare. First of all, you need to ensure that the custom content is actually loaded, and then the role of declare is that you don't have to declare the import every time.

// app.tsx
const LANG = 'zh-CN'; // This is a top-level scope variable, available globally
// any.d.tsx
declare const LANG: string;
// other.tsx
const currentLang = userLang || LANG; // don't need import LANG from app.tsx了
  • Related