TS2539
Cannot assign to
defaultNamebecause it is not a variable.
Broken Code ❌
export let defaultName = 'Benny';import { defaultName } from './defaults';
defaultName = 'Sofia';Fixed Code ✔️
Using the let keyword might lead to the assumption of exporting a variable, but in reality, every export becomes a constant. As we're aware, constants cannot be reassigned.
To work around this, we can use a little trick with how some types are handled. If we put our defaultName (initially a primitive string type) inside an object (which is a complex type), we can change the value inside the object as it is passed by reference:
export const defaults = {
defaultName: 'Benny',
};import { defaults } from './defaults';
defaults.defaultName = 'Sofia';