Im struggling to understand why the following doesnt throw an error
type Callback<T> = () => T
function format<V>(callback: Callback<V>): V {
return callback()
}
type Test = {foo: string}
format<Test>(() => {
return {
foo: 'hello',
bar: 'dd' // I expect an error to be here as `bar` does not exist on type Test
}
})
// if I explicitly set the return type in the callback then I get the correct error
format<Test>((): Test => {
return {
foo: 'hello',
bar: 'dd' // this now errors as I have set the return type.
}
})
I cant help but feel this is a duplication?
Is this a typescript limitation and is "as expected", or are my types incorrect?
¯\_(ツ)_/¯formatrequires to be explicitly set a return type to show the error and why the first case is not showing the error? OP feels like typingTesttwice should not be required (the later case) whenformatfunction clearly indicates what the return type is.typescriptlimitiation.