I have an edit state that returns an edit for a single key out of many.
<code>type EditState_Empty = {
name: null
value: null
}
type EditState_InProgress = {
name: 'name1' | 'name2'
value: string
}
type EditState = EditState_Empty | EditState_InProgress
type AfterEditParams = {name1: string} | {name2: string}
const initialState = {
name: null, value: null
}
const reducer = (prev: EditState, action: string): EditState => {
switch (action) {
case 'name1':
return {
name: 'name1',
value: 'some-value'
} as const
case 'name2':
return {
name: 'name2',
value: 'some-value'
} as const
default:
return initialState
}
}
const saveEdit = (editState: EditState_InProgress): void => {
const editParam: AfterEditParams = {
[editState.name]: editState.value,
} as const
}
</code>
<code>type EditState_Empty = {
name: null
value: null
}
type EditState_InProgress = {
name: 'name1' | 'name2'
value: string
}
type EditState = EditState_Empty | EditState_InProgress
type AfterEditParams = {name1: string} | {name2: string}
const initialState = {
name: null, value: null
}
const reducer = (prev: EditState, action: string): EditState => {
switch (action) {
case 'name1':
return {
name: 'name1',
value: 'some-value'
} as const
case 'name2':
return {
name: 'name2',
value: 'some-value'
} as const
default:
return initialState
}
}
const saveEdit = (editState: EditState_InProgress): void => {
const editParam: AfterEditParams = {
[editState.name]: editState.value,
} as const
}
</code>
type EditState_Empty = {
name: null
value: null
}
type EditState_InProgress = {
name: 'name1' | 'name2'
value: string
}
type EditState = EditState_Empty | EditState_InProgress
type AfterEditParams = {name1: string} | {name2: string}
const initialState = {
name: null, value: null
}
const reducer = (prev: EditState, action: string): EditState => {
switch (action) {
case 'name1':
return {
name: 'name1',
value: 'some-value'
} as const
case 'name2':
return {
name: 'name2',
value: 'some-value'
} as const
default:
return initialState
}
}
const saveEdit = (editState: EditState_InProgress): void => {
const editParam: AfterEditParams = {
[editState.name]: editState.value,
} as const
}
TypeScript tells me:
<code>Type '{ readonly [x: string]: string; }' is not assignable to type 'AfterEditParams'.
Property 'name2' is missing in type '{ readonly [x: string]: string; }' but required in type '{ name2: string; }'.
</code>
<code>Type '{ readonly [x: string]: string; }' is not assignable to type 'AfterEditParams'.
Property 'name2' is missing in type '{ readonly [x: string]: string; }' but required in type '{ name2: string; }'.
</code>
Type '{ readonly [x: string]: string; }' is not assignable to type 'AfterEditParams'.
Property 'name2' is missing in type '{ readonly [x: string]: string; }' but required in type '{ name2: string; }'.
Here is the example on TypeScript playground:
Will someone please explain why the error occurs and how to fix it?