MJDevelops
MJDevelops

Reputation: 75

solving assignment error in typescript: «Module parse failed»

I'm trying to assign a object to initialState variable, the selectedActivity type is Activity | undefined and after Nullish Coalescing operator (??) the emptyActivity is of type Activity. but when this line execute, it throws an error that says: Module parse failed: Unexpected token You may need an appropriate loader to handle this file type. Code:

interface Props {
    selectedActivity: Activity | undefined
    closeForm: () => void
}
export default function ActivityForm({ selectedActivity, closeForm }: Props) {
    const emptyActivity: Activity = {
        id: '',
        title: '',
        date: '',
        description: '',
        category: '',
        city: '',
        venue: '',
    }
const initialState = selectedActivity ?? emptyActivity;

and this is Activity interface:

export interface Activity {
    id: string
    title: string
    date: string
    description: string
    category: string
    city: string
    venue: string
}

I'm coding a react project and I want to solve the assignment error.

Upvotes: 0

Views: 84

Answers (1)

Mina
Mina

Reputation: 17434

As the error said that your loader can't handle this syntax, maybe it is old, and you need to update it, but as a temporary solution you can convert this syntax to ternary operator syntax and it should work.

const initialState = selectedActivity ? selectedActivity : emptyActivity;

Upvotes: 1

Related Questions