Imagine I define an object where each key is a string and the corresponding value is a function that takes a number and returns nothing:
let funcMap: { [id: string]: (num: number) => void };
When I assign functions to this object, it seems like type checking is not effectively enforced:
funcMap = {
// This works fine
'one': (num: number) => { },
// I would anticipate an error here due to missing function parameter
'two': () => { },
// I would expect an error because the function returns a number
'three': () => 0
};
Nevertheless, when calling these functions, type checking is indeed enforced:
// This works as expected
funcMap['two'](0);
// This results in an error, which is what we want
funcMap['two']();
Consider this additional scenario:
class Test {
constructor(public func: (num: number) => void) { }
}
// I would anticipate an error here
let x = new Test(() => { });
// Expected behavior, no issues
x.func(0);
// As predicted, this is an error
x.func();
Is this the intended behavior?