-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Open
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
π Search Terms
infer return type function array property optional
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about "return", "property", "properties", "infer"
β― Playground Link
π» Code
interface Fix {
message?: string;
}
type Check = () => Fix;
const checks: Check[] = [
() => ({}),
() => ({ message: 555 }), // no error reported
];
π Actual behavior
No error is reported on the last item of the array.
π Expected behavior
It should report an error, because number 555 is not a legal value for optional property message
of type string.
Additional information about the issue
Workaround 1: add explicit return type
const checks: Check[] = [
() => ({}),
(): Fix => ({ message: 555 }), // Type 'number' is not assignable to type 'string'
];
Workaround 2: add "as const"
const checks: Check[] = [
() => ({}),
() => ({ message: 555 }), // Type 'number' is not assignable to type 'string'
] as const;
Workaround 3: remove first item
const checks: Check[] = [
() => ({ message: 555 }), // Type 'number' is not assignable to type 'string'
];
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created