skip to Main Content

I have this array of Projects where stack has multiple strings

const projects: IProject[] = [
    {name: '', description: '', stack: {'php', 'sql'}}
  ]

How will the interface be or what is the best approach?

interface IProject {
  name: string
  description: string
  stack: 
  repo?: string
  url?: string
}

I tried to declare another interface IStack[] but I don’t want another array of key value pairs as it increases my code

stack: [
{name: 'react'}, {name: 'graphql'}
]

2

Answers


  1. If what you want is a fixed set of strings, you can use a union type:

    type Stack = "php" | "sql" | "react";
    
    interface Project {
      stack: Stack[];
    }
    
    const project: Project = {stack: ["php", "sql"]};
    

    Otherwise if you just want an array of random strings, you can use stack: string[]

    Login or Signup to reply.
  2. You can use in string array:

    interface IProject {
      name: string
      description: string
      stack: string[]
      repo?: string
      url?: string
    }
    

    and the object is:

    const projects: IProject[] = [
        {name: '', description: '', stack: ['php', 'sql']}
      ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search