interface Example {
value?: string
[prop: string]: any
}
const xxx: Example = { name: 'Thomas' }
const v = xxx.name
Adding "any" here actually removes the inferred "string" type on name that would otherwise be there if xxx was not assigned to Example.
Is there any way to have a interface or type that passed the inferred type?
This can be done using a generic
Recordtype:To avoid having to write the explicit type parameters in
Example<'name', string>, you can use a generic identity function:The constant
xxxwill be inferred to have the typeExample<'name', string>as required.