I am currently working on defining an interface named ParsedArguments
to assign properties to an object, and here is what it looks like:
import {Rules} from "../Rules/types/Rules";
export interface ParsedArguments {
//other props
//...
rules?: Rules,
}
In another function, I am attempting to create an object that retrieves values from cli
and generates an object that complies with this interface. Here's a snippet of how I'm trying to achieve that:
private parseCliArgs(args: string[]): ParsedArguments {
let res: ParsedArguments = {}
for (const arg of args) {
if (arg.startsWith('--')) {
const {key, value} = this.getArgValues(arg)
if (!res.rules) {
res.rules = {}
}
const rule: keyof Rules = key
if (res.rules[rule]) {
//HERE I GET THE ERROR
//TS2322: Type 'STRING' is not assignable to type 'undefined'.
res.rules[rule] = value
}
}
}
return res
}
Even after checking if the property of Rules
exists, I still encounter the same error. I haven't been able to resolve it yet and would really appreciate your insights on this issue. This is how the Rules
interface is structured:
export interface Rules {
someProp1?: boolean,
someProp2?: number,
//...
}
I also experimented with using const rule = key as keyof Rules
instead of const rule: keyof Rules = key
, but it didn't make any difference.
If you require further clarification or if I missed any crucial information, please let me know. Thank you in advance.