-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Spreading possibly undefined variable results in empty object type union #18000
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Just to note it works without error in playground. Feels like some sort of regression. |
I tested 2.4.2, 2.5.0 and 2.5.1. All of them produce this error. |
I'm new to TypeScript, but enjoying it immensely - Thank you! I believe I've just run into the same problem with typescript 2.5.2 . export interface IFormAction {
type: FormActions // an enum not shown here
}
export interface IFormState {
[formName: string]: {
[fieldName: string]: string
}
}
export interface IFormReducer {
(state?: IFormState, action?: IFormAction): IFormState
}
export const reducer: IFormReducer = (
state, action = { type: FormActions.none }
) => {
const defaultState: IFormState = {}
return {
...defaultState,
...state,
}
} src/forms/reducer.ts(42,14): error TS2322: Type '(state: IFormState | undefined, action?: IFormAction | undefined) => {} | { [x: string]: { [field...' is not assignable to type 'IFormReducer'.
Type '{} | { [x: string]: { [fieldName: string]: string; }; }' is not assignable to type 'IFormState'.
Type '{}' is not assignable to type 'IFormState'.
Index signature is missing in type '{}'. As noted by @ajafff , this seems to be related to object spreads, as removing them removes the error. export const reducer: IFormReducer = (
state, action = { type: FormActions.none }
) => {
const defaultState: IFormState = {}
return defaultState // compiles no problem
} Note that explicitly adding export interface IFormReducer {
(state?: IFormState, action?: IFormAction): IFormState | {}
}
// reducer uses spread as before, but compiles without issue Hope this is helpful. |
Duplicate of #16694 |
TypeScript Version: 2.5.1
Code
Expected behavior:
No Error
Actual behavior:
The text was updated successfully, but these errors were encountered: