Theo's Typesafe CultTTC
Theo's Typesafe Cult3y ago
12 replies
alan

how to fix typescript error

I have this code:
async function updateMysql({config}: {config: {
    name: string;
  }}) {}

async function updateMongo({config}: {config: {datasourceName: string;} }) {}

type Configs = {
mysql: {
    name: string;
},
mongodb: {
    datasourceName: string;
}
}

const dbTypes = [
    {
        type: 'mysql',
        caller: updateMysql
    },
    {
        type: 'mongodb',
        caller: updateMongo
    }
]

function processConfig(config: Configs[keyof Configs], dbType: keyof Configs) {
    const typeConfig = dbTypes.find(({type}) => type === dbType)!;
    if (dbType === 'mysql'){
      typeConfig.caller({config: config as Configs['mysql']});
    } else {
        typeConfig.caller({config: config as Configs['mongodb']});
    }    
}

but I'm getting this error:
Type '{ datasourceName: string; }' is not assignable to type '{ name: string; } & { datasourceName: string; }'.
  Property 'name' is missing in type '{ datasourceName: string; }' but required in type '{ name: string; }'.(2322)
input.tsx(2, 5): 'name' is declared here.
input.tsx(1, 39): The expected type comes from property 'config' which is declared here on type '{ config: { name: string; }; } & { config: { datasourceName: string; }; }'
(property) config: {
    name: string;
} & {
    datasourceName: string;
}

how would I fix this? I would have thought that config would be a union type and not an intersect type
Was this page helpful?