I recently utilized a module that had the capability to perform a certain task
function print(obj, key) {
console.log(obj[key])
}
print({'test': 'content'}, '/* vs code will show code recommendation when typing */')
I am eager to implement this feature in my project, but unfortunately, I cannot remember the name of the module and I am uncertain if my memory serves me right (perhaps it's not even possible).
Currently, I am developing a package and have written some code like this:
interface Box {
content: {
[key: string]: string
}
using: string // key of content
}
const box: Box = {
content: {
'something': 'inside'
},
using: 'something'
}
function showBox(box: Box) {
console.log(box.content[box.using])
}
The content
object actually originates from another package using Typescript. Ideally, I would prefer not to encapsulate the type.
In order to aid developers in identifying bugs while coding, I am investigating whether there is a method to validate an invalid Box
type like so:
const box: Box = {
content: {
'something': 'inside'
},
using: 'samething' // trigger an error during type checking
}
Alternatively, I am searching for any approach that can enable IDEs to provide code recommendations indicating that using
should be a key within the content
object.
At present, my code appears as follows, but it does not align with my desired outcome, and I am stuck on how to proceed further
interface Box {
content: {
[key: string]: string
}
using: keyof Box['content'] // will result in string | number
}
A big thank you to everyone who responds!