Currently, I am developing an extension for Arduino on VSCode and facing an issue with a section of my code. To load the project's configuration, I am accessing a .json file located in the .vscode folder. While ideally, the user should not manually edit this file, there is a possibility that they may input values that are not intended to be included.
I have created an interface named Config to capture the configuration data as shown below:
interface Config {
port?: string,
baud?: number,
board?: string
}
let myConfig: Config = JSON.parse(fs.readFileSync(`path/to/my/config.json`).toString());
For instance, if my config looks like this:
{ "test": "hi!" }
Despite specifying myConfig
with the type Config, upon loading the config and displaying it using console.log, I observe:
{test: 'hi!'}
I have attempted modifying Config into both a class and a type but have been unable to prevent unexpected additional properties in my config. Is there a method to efficiently detect inconsistencies or utilize a try {} catch {} statement to identify when a variable does not align with its corresponding interface structure?