Reputation: 125
Asking this here because not sure how to phrase it properly so google understands.
I have a utility function to capitalize words in a string, that as a second argument can get passed an options object:
export type Options = {
onlyFirstWord?: boolean;
separator?: string;
};
const defaultOptions = {
onlyFirstWord: false,
separator: " ",
};
export default function capitalize(
str: string,
options: Options = defaultOptions
) {}
So the second argument is optional, since I've provided a default value for it, but if I make the properties optional in the type, it requires me to check if those are undefined or not, even though they 100% can never be undefined, because of the default value. But if I don't make them optional, I will have to enter every single property when passing the options object, otherwise it would give an error.
I know there is a lot of similar questions, but in this case, is there no way to tell typescript that those values are never undefined, other than having to put exclamation marks or use optional chaining? Shouldn't it automatically figure out that they are never undefined since I'm providing a default value for it? Thanks and do let me know if this is a duplicate and this has already been answered.
Upvotes: 3
Views: 3396
Reputation: 2637
Here is a playground link to a working solution.
The code is copied below, with comments:
// remove the ?'s from the Options type
type Options = {
onlyFirstWord: boolean;
separator: string;
};
const defaultOptions = {
onlyFirstWord: false,
separator: "default",
};
// use this function to merge default options with Partial options
const createDefaultOptions = (options: Partial<Options>) => {
return {...defaultOptions, ...options};
}
// allow the capitalize function to take Partial options
function capitalize(
str: string,
options: Partial<Options> = defaultOptions
) {
// merge the given options with the partial options
const opts = createDefaultOptions(options);
// print the result
console.log(`${str}: ${opts.onlyFirstWord}, ${opts.separator}`);
}
// tests
capitalize('nothing');
capitalize('first', {onlyFirstWord: true});
capitalize('second', {separator: 'j'});
capitalize('both', {onlyFirstWord: true, separator: 'nice'});
Output:
nothing: false, default
first: true, default
second: false, j
both: true, nice
Upvotes: 4