1 year ago

#382203

test-img

QTPah

Typescript SyntaxError: Unexpected identifier with basic syntax

I am trying to run a simple number prediction code that i wrote in typescript. But for some reason, i get an error on basic typescript syntax. (i am quite new to typescript)

I have tried to change the ecmascript version to 7 ("ES2017") but the error still occures.

I also tried reinstalling typescript.

index.ts:

type Cell = { val : string, weight: number }

let data : Array<Cell> = [];

function cicle() {
    process.stdin.once('data', (buffer) => {

        // Get input and increase it's weight

        const input = buffer.toString('utf-8').substring(0, buffer.toString('utf-8').length - 1);

        let cell = data.find(c => c.val === input);

        // Add cell to array if it doesn't exist yet
        if(!cell) {
            data.push({ val: input, weight: 0 });
            cell = data.find(c => c.val === input);
        }

        cell.weight += 0.1;

        // Predict next input

        console.log(`Prediction: ${data.find(c => c.weight === Math.max(...data.map(c => c.weight)))}
    Enter number: `);
        cicle();
    });
}

cicle();

tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "esModuleInterop": true,
        "target": "es6",
        "moduleResolution": "node",
        "sourceMap": true,
        "outDir": "./dist"
    },
    "lib": ["es6"]
}

Error:

index.ts:1
type Cell = { val : string, weight: number }
     ^^^^

SyntaxError: Unexpected identifier
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1032:15)
    at Module._compile (node:internal/modules/cjs/loader:1067:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

typescript

syntax-error

0 Answers

Your Answer

Accepted video resources