localForage 类型未知

localForage type is unkown

const AddMovieToDashboardFn = (movie) => {
  localForage.getItem('my array').then((value) => {
    const x: string[] = value;
    console.log(x)
  });
}

const x 有语法错误:

Type 'unknown' is not assignable to type 'string[]'.

then 回调中的值参数是 unknown 类型,因此错误是有道理的。如果我喜欢 value 它会显示一个数组。如何给then回调中的value一个类型,应该是数组类型。

试试这个:

const AddMovieToDashboardFn = (movie) => {
  localForage.getItem('my array').then((value: string[]) => {
    const x = value;
    console.log(x)
  });
}

或者,由于 localforage 的输入允许传递类型参数 (https://github.com/localForage/localForage/blob/master/typings/localforage.d.ts):

const AddMovieToDashboardFn = (movie) => {
  localForage.getItem<string[]>('my array').then((value) => {
    const x = value;
    console.log(x)
  });
}

(感谢@Elias Schablowski 在评论中提出的建议)