Skip to content

Instantly share code, notes, and snippets.

@pbroschwitz
Last active May 25, 2022 13:43
Show Gist options
  • Save pbroschwitz/972fffef525cef90b7a51ded77f7e606 to your computer and use it in GitHub Desktop.
Save pbroschwitz/972fffef525cef90b7a51ded77f7e606 to your computer and use it in GitHub Desktop.
Object literal may only specify known properties in TypeScript - ts(2345)

Problem

const someArray = ['foo', 'bar']
const someOtherArray = ['foobar', 'barfoos']

const config = [
    ...someArray,
    ...someOtherArray,
    {
        name: 'someName',
        type: 'someType'
    }
]

config.push({
    name: 'someNewName',
    type: 'someNewType',
    newProperty: { someThing: 'value' } 
})
const someArray = ['foo', 'bar'] const someOtherArray = ['foobar', 'barfoos']

const config = [ ...someArray, ...someOtherArray, { name: 'someName', type: 'someType' } ]

config.push({ name: 'someNewName', type: 'someNewType', newProperty: { someThing: 'value' } })


Solution Step 1

const someArray = ['foo', 'bar']
const someOtherArray = ['foobar', 'barfoos']

export type ConfigType = (string | {
    name: string;
    type: string;
    newProperty?: {
      someThing: string;
    }
})[]

const config: ConfigType = [
    ...someArray,
    ...someOtherArray,
    {
        name: 'someName',
        type: 'someType'
    }
]

config.push({
    name: 'someNewName',
    type: 'someNewType',
    newProperty: { someThing: 'value' } 
})

console.log(config)
const someArray = ['foo', 'bar'] const someOtherArray = ['foobar', 'barfoos']

export type ConfigType = (string | { name: string; type: string; newProperty?: { someThing: string; } })[]

const config: ConfigType = [ ...someArray, ...someOtherArray, { name: 'someName', type: 'someType' } ]

config.push({ name: 'someNewName', type: 'someNewType', newProperty: { someThing: 'value' } })

console.log(config)


Going a step further - Typing type

const someArray = ['foo', 'bar']
const someOtherArray = ['foobar', 'barfoos']

export type TypeTypes = 'someString' | 'someOtherString';

export type ConfigType = (string | {
    name: string;
    type: TypeTypes;
    newProperty?: {
      someThing: string;
    }
})[]

const config: ConfigType = [
    ...someArray,
    ...someOtherArray,
    {
        name: 'someName',
        type: 'someString'
    }
]

config.push({
    name: 'someNewName',
    type: 'someOtherString',
    newProperty: { someThing: 'value' } 
})

console.log(config)
const someArray = ['foo', 'bar'] const someOtherArray = ['foobar', 'barfoos']

export type TypeTypes = 'someString' | 'someOtherString';

export type ConfigType = (string | { name: string; type: TypeTypes; newProperty?: { someThing: string; } })[]

const config: ConfigType = [ ...someArray, ...someOtherArray, { name: 'someName', type: 'someString' } ]

config.push({ name: 'someNewName', type: 'someOtherString', newProperty: { someThing: 'value' } })

console.log(config)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment