Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6208

Javascript: Unexpected token { - occurs when try to import custom class in typescript file into javascript file

$
0
0

I am writing simple CLI scripts for react-native project. But I am not available to import class from typescript to javascript. It throw me following error.

import { TestResult } from './src/lib/modules/test-result';
SyntaxError: Unexpected token {...

package.json

...
"script": {
...
"formular:convert": "node convertor.js"
...
}
...

convertor.js

import {TestResult} from './src/lib/models/test-result';
...

test-result.ts

import uuid from 'uuid/v4';

import { Exception } from '../../errors';
import { OilTypes, RawMaterialTypes } from '../oils';

import { CalculatedResult } from './calculated-result';
import { DeviceSettings } from './device-settings';
import { DeviceStatus } from './device-status';
import { DisplayedResult } from './displayed-result';
import { Measurement } from './measurement';
import { PhoneInfo } from './phone-info';
import { PrimaryCompound } from './primary-compound';

export class TestResult {
    constructor(
        public id: string = uuid(),
        public createdAt: Date = new Date(),
        public updatedAt: Date = new Date(),
        public errors: Exception[] = [],
        public alias: string = '',
        public analyteTemp: number = 75,
        public flowerWeight: number = 0,
        public hidden: boolean = false,
        public note: string = '',
        public oilType: OilTypes = OilTypes.OliveOil,
        public primaryCompound: PrimaryCompound = PrimaryCompound.UNDEFINED,
        public units: string = '',
        public user: string = '',
        public air310Meas: Measurement = new Measurement(),
        public air280Meas: Measurement = new Measurement(),
        public analyte310Meas: Measurement = new Measurement(),
        public analyte280Meas: Measurement = new Measurement(),
        public calculated: CalculatedResult = new CalculatedResult(),
        public displayed: DisplayedResult = new DisplayedResult(),
        public status: DeviceStatus = new DeviceStatus(),
        public settings: DeviceSettings = new DeviceSettings(),
        public phoneInfo: PhoneInfo = new PhoneInfo(),
        public rawMaterialType: RawMaterialTypes = RawMaterialTypes.Isolate,
    ) {
    }
}

tsconfig.json

{
    "compilerOptions": {
        /* Basic Options */
        "target": "es5",                       /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
        "module": "commonjs",                  /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
        // "lib": [],                             /* Specify library files to be included in the compilation. */
        "allowJs": true,                       /* Allow javascript files to be compiled. */
//        "checkJs": true,                       /* Report errors in .js files. */
        "jsx": "react",                 /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
                            /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
        "removeComments": true,                /* Do not emit comments to output. */
        "noEmit": true,                        /* Do not emit outputs. */


        /* Strict Type-Checking Options */
        "strict": true,                        /* Enable all strict type-checking options. */

        /* Additional Checks */
        "noUnusedLocals": true,                /* Report errors on unused locals. */
        "noUnusedParameters": true,            /* Report errors on unused parameters. */
        "noImplicitReturns": true,             /* Report error when not all code paths in function return a value. */
        "noFallthroughCasesInSwitch": true,    /* Report errors for fallthrough cases in switch statement. */

        /* Module Resolution Options */
        "moduleResolution": "node",            /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
        "baseUrl": "./",                       /* Base directory to resolve non-absolute module names. */
        "types": [                             /* Type declaration files to be included in compilation. */
            "jest",
            "mocha",
            "detox"
        ],
        "allowSyntheticDefaultImports": true,  /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
        "esModuleInterop": true,               /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */

        /* Experimental Options */
        "experimentalDecorators": true,        /* Enables experimental support for ES7 decorators. */
        "emitDecoratorMetadata": true,         /* Enables experimental support for emitting type metadata for decorators. */
        "skipLibCheck": true,                  /* Skip type checking of declaration files. Requires TypeScript version 2.0 or later. */
        "forceConsistentCasingInFileNames": true,/* Disallow inconsistently-cased references to the same file. */
        "resolveJsonModule": true              /* Include modules imported with '.json' extension. Requires TypeScript version 2.9 or later. */
    },
    "exclude": [
        "node_modules",
        "babel.config.js",
        "metro.config.js",
        "jest.config.js"
    ]
}

I stuggled to find the solution, and found some answers here and there, but all of them doesn't work.

Thanks for your help.


Viewing all articles
Browse latest Browse all 6208

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>