In an attempt to explain a specific partial type of return value for a functional interface, I have encountered an issue.
Within my IStore
interface, there is only one property called test
. When assigning this interface to the function foo
, which returns a hashmap with additional properties, TypeScript does not raise any errors. However, I require TypeScript to throw an error when the returned value from foo
does not strictly match with Partial<IStore>
. This should happen without explicitly specifying the return value for foo
.
interface IStore {test: string;}
type IFunction<S> = (store: S) => Partial<S>;
// No TypeScript errors are raised. Why?
// This is unsatisfactory for me.
const foo1: IFunction<IStore> = () => ({
test: '',
test2: '' // why no errors in this row?
});
// TypeScript error,
// It is functional, but not meeting my requirements
const foo2: IFunction<IStore> = (): IStore => ({
test: '',
test2: '' // error here
});
// In contrast...
// No TypeScript errors are raised
// This is acceptable
const foo3: IFunction<IStore> = () => ({
test: ''
});
// Additionally...
// TypeScript error: Type '{ test2: string; }' has no properties
// in common with type 'Partial<IStore>'
// This is acceptable
const foo4: IFunction<IStore> = () => ({
test2: ''
});
How can I trigger an error similar to "case 2" (foo2
) in "case 1" (foo1
) without using ... (): IStore => ...
?