Theo's Typesafe CultTTC
Theo's Typesafe Cult3y ago
49 replies
Perfect

Battling TypeScript Types

I have been struggling with this typescript issue for a bit now and not sure how to fix.

I have an object containing some data that I import. It looks like this. boom has a crazy long type considering each of the objects in the array. (where I think this is making things hard).

export const boom = [
  {
    name: "Rocket",
    cost: {
      sulfur: 1400,
      charcoal: 1950,
      frags: 100,
      low_grade: 30,
      pipes: 2,
    },
  },
  {
    name: "C4",
    cost: {
      sulfur: 2200,
      charcoal: 3000,
      frags: 200,
      low_grade: 60,
      cloth: 5,
      tech_trash: 2,
    },
  },
  {
    name: "Satchel",
    cost: {
      sulfur: 480,
      charcoal: 720,
      frags: 80,
      cloth: 10,
      rope: 1,
    },
  },
  {
    name: "Explosive Ammo",
    cost: {
      sulfur: 25,
      charcoal: 30,
      frags: 5,
    },
  },
  {
    name: "Beancan Grenade",
    cost: {
      sulfur: 120,
      charcoal: 180,
      frags: 20,
    },
  },
  {
    name: "Incendiary Rocket",
    cost: {
      sulfur: 610,
      charcoal: 900,
      frags: 10,
      low_grade: 253,
      pipes: 2,
    },
  },
  {
    name: "High Velocity Rocket",
    cost: {
      sulfur: 200,
      charcoal: 300,
      pipes: 1,
    },
  },
  {
    name: "Handmade Shell",
    cost: {
      sulfur: 5,
      charcoal: 8,
      stone: 3,
    },
  },
];


The type for each item is automatically created, but I would rather it just be name: string and cost: Record<string, number>. How can I just simplify the types? My app works fine but I just cant get rid of this typescript error. Thanks!
image.png
image.png
Was this page helpful?