I'm trying to perform a simple test of an interface. The code is as follows:
interface TestInterface {
id: number;
text: string;
}
const testInterfaceImplementation: TestInterface = {
id: 1,
text: 'sample text'
};
console.log(testInterfaceImplementation.text);
When I run this code with Node.js configuration I get this error:
interface TestInterface {
^^^^^^^^^^^^^
SyntaxError: Unexpected identifier
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
When I run the code without this interface it works fine:
const testInterfaceImplementation = {
id: 1,
text: 'sample text'
};
console.log(testInterfaceImplementation.text);
What is the problem? I've also tried to move the interface to a different .ts file but the error still occurs.
tsconfig.json file:
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"sourceMap": true
},
"exclude": [
"node_modules"
]
}
run_configuration
You can't run the Typescript code by passing it to Node.js directly, Node.js doesn't provide native support for executing Typescript. The code has to be either compiled on-the-fly or precompiled. here are some recipes:
To run a selected TypeScript file using ts-node:
npm i ts-node.--require ts-node/register to the Node parameters field.$FilePathRelativeToProjectRoot$.If you need to pass any additional parameters to ts-node (e.g. --project tsconfig.json), you can add them to the Application parameters field in the run/debug configuration.
To compile app with TypeScript and run a selected TypeScript file
tsconfig.json..js file.$FileRelativeDir$/$FileNameWithoutExtension$.jsbuild/$FileRelativeDir$/$FileNameWithoutExtension$.jsIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With