简体   繁体   中英

Nuxt3 using typescript with useFetch and transform option

I'm using useFetch to fetch an array of strings string[] with the transform option to transform it from array to object.

I tried the code below:

type ReceivedData = [string, string, string];

interface TransformedData {
    prop1: string,
    prop2: string,
    prop3: string
}

const { data, error } = await useFetch<ReceivedData>(
    `./jsonPath.json`,
    {
      transform: ([prop1, prop2, prop3]: ReceivedData): TransformedData  => ({
         prop1, prop2, prop3
      })
    }
);

I'm getting this error

Type '([prop1, prop2, prop3]: ReceivedData) => TransformedData' is not assignable to type '(res: ReceivedData) => ReceivedData'.

I pretty sure I'm messing something here, but Typescript is expecting the transform method to return the same data type it receives.

Currently, you could use it like this:

  type ReceivedData = [string, string, string]

  interface TransformedData {
    prop1: string
    prop2: string
    prop3: string
  }

  const transform = ([prop1, prop2, prop3]: ReceivedData): TransformedData => ({
    prop1,
    prop2,
    prop3,
  })

  const { data } = await useFetch<
    ReceivedData,
    Error,
    string,
    ReceivedData,
    typeof transform
  >(`./jsonPath.json`, {
    transform,
  })

Honestly speaking, I hope this will be improved in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM