'Re-assign a function to the exports when import as a namespace
a.ts:
export const add = () => {
return 'ok';
};
I am trying to re-assign a new add function like this:
import * as a from './a';
a.add = () => {
return 'not ok';
};
console.log(a.add());
TSC throws an error:
Cannot assign to 'add' because it is a read-only property.ts(2540)
It works fine when I use es6. How to correct the TS type of the namespace a so that I can re-assign a new function with the same signature?
I don't want to do this: //@ts-ignore and (a as any).add = () => {}. It may be related to ts config.
tsconfig.json:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"lib": [
"ES2015",
"es2016",
"dom",
"es2018.promise"
],
"allowJs": true,
"jsx": "react",
"outDir": "./build",
"rootDir": ".",
"noImplicitAny": false,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"noImplicitThis": true,
"alwaysStrict": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"resolveJsonModule": true
},
"exclude": [
"dist",
"docs",
"build",
"node_modules"
]
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
