I am creating simple function to count all existing vowels in given string. I wrote function to do it but I cannot compile this with Typescript version higher than 3. I am curious how this function should be written to pass Typescript versions higher than 3.
function getCount(str: string = ''): number {
return str.match(/[aeiou]/gi).length;
}
test:
describe("getCount", function(){
it ("should pass a sample test", function(){
assert.strictEqual(getCount("abracadabra"), 5)
});
});
With Typescript version 2.4 everything passes but with higher versions I am getting this:
error TS2531: Object is possibly 'null'.
str, since you're counting the number of vowels in a string, the string should be required? Maybe consider removing the initial value