Здравствуйте!
В данной строке:
{[key in typeof parameters[number]['name']]: string | null}
возникает ошибка: TS2322: Type 'Names' is not assignable to type 'string | number | symbol'.
Подскажите, пожалуйста, как её исправить?
type RegexpParameter<Names> = {
name:Names,
type: 'd'|'w'|'ss'
}
function getParameters<Names>(substring:string,parameters:RegexpParameter<Names>[]):{[key in typeof parameters[number]['name']]: string | null}{
const results:any= {}
function getRegExp(type:RegexpParameter<any>['type']){
switch (type){
case "d": return '\\d+'
case "w": return '\\w+'
case "ss": return '[^,]+'
}
}
parameters.forEach(value=>{
const regexp = new RegExp(`(?<=${value.name}:\\s?)${getRegExp(value.type)}`,'g')
const match = regexp[Symbol.match](substring)
results[value.name]=match?match[0].trim():null;
})
return results
}
const imgParameters:RegexpParameter<'w'|'h'|'c'|'p'>[] = [{name:'w',type:'d'},{name:'h',type:'d'},{name:'c',type:'ss'},{name:'p',type:'w'}]
const {w:width,h:height,c:caption,p:priority} = getParameters(substrings[1],imgParameters)