I'm pretty new to typescript, this was probably answered before, but I can't find the answer anywhere, so here is the problem I'm trying to solve.
I have an enum
enum TableNames = {
example: 'example'
example2: 'example2',
}
and I have these types for my models
type TableName = keyof typeof TableNames
type TableState<L =any> = {
list: Array<L>
}
type ExampleDataType = {
id: string
...
}
type ExampleData2Type = {
id: number
...
}
type TableStateType = {
example: TableState<ExampleDataType>
example2: TableState<ExampleData2Type>
}
type InterListType<TName extends TableName> = TableStateType[TName]['list'][number]
and I have this special function
const createPath = <TName extends TableNames, T = InferListType<TName>>(
tableName: TName,
record: T
) => {
if (tableName === TableNames.example) {
console.log(record) // how to get this to infer to "ExampleDataType"
}
if (tableName === TableNames.example2) {
console.log(record) // how to get this to infer to "ExampleData2Type"
}
}
The question is the comment above, right now I'm getting this
if (tableName === TableNames.example) {
console.log(record) // T = InferListType<TName>
}
NOTE: typescript version is 3.7.4