I'm working on a function that accepts parameters of type Record<string, string>
. How can I define a type with a limited set of indexes without triggering a TypeScript compiler error?
Is there a way to create an interface with only specific index keys of a certain type?
type MyRecord = Record<string, string>
interface MyA {
index1: string
index2: 'true' | 'false'
}
function fn(a: MyRecord) {
//perform operations
}
const myA: MyA = {
index1: 'index',
index2: 'true'
}
fn(myA) // throws error: Index signature is missing in type 'MyA'.
It seems like the solution involves changing MyA to be type My = {...}
.