Custom merge that doesn't override values with undefined #25
RebeccaStevens
started this conversation in
Show and tell
Replies: 2 comments 4 replies
-
@RebeccaStevens The example seems have error. |
Beta Was this translation helpful? Give feedback.
3 replies
-
Continue the above example with the additional option I create an usual example: merge import { deepmerge } from './deepmerge';
interface Options {
opt1?: string;
opt2?: boolean;
opt3?: boolean;
opt4?: string[];
}
const defaultOptions = {
opt3: true,
opt4: [] as string[],
};
type MergedOptions = typeof defaultOptions & Options;
export class MyEditor {
#options;
#options2;
constructor(options: Options) {
this.#options = deepmerge(defaultOptions, options);
// ==> Wrong: vscode recognizes `opt4` as `string[] | undefined`
if (this.#options.opt4.length > 2) {
// do something
}
this.#options2 = deepmerge(defaultOptions, options) as MergedOptions;
// ==> Expected: vscode recognizes `opt4` as `string[]`
if (this.#options2.opt4.length > 2) {
// do something
}
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Beta Was this translation helpful? Give feedback.
All reactions