Skip to content

Instantly share code, notes, and snippets.

@sean-nicholas
Last active July 8, 2019 11:32
Show Gist options
  • Select an option

  • Save sean-nicholas/f62ea2d353c57c3dd1f7fd4ef3e6c7d8 to your computer and use it in GitHub Desktop.

Select an option

Save sean-nicholas/f62ea2d353c57c3dd1f7fd4ef3e6c7d8 to your computer and use it in GitHub Desktop.
dynamic key
interface IdMappingDoc {
firstId: string,
secondId: string,
}
interface IdMappingConfig {
sourceIdField: 'firstId' | 'secondId'
targetIdField: 'firstId' | 'secondId'
}
const idMappingConfig: IdMappingConfig = {
sourceIdField: 'firstId',
targetIdField: 'secondId'
}
function doStuff(idMapping: IdMappingDoc) {
}
// ERROR:
// Argument of type '{ [x: string]: string; }'
// is not assignable to parameter of type 'IdMappingDoc'.
// Type '{ [x: string]: string; }' is missing the following
// properties from type 'IdMappingDoc': firstId, secondId
doStuff({
[idMappingConfig.sourceIdField]: 'foo',
[idMappingConfig.targetIdField]: 'bar'
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment