export class User {
firstName: string;
lastName: string;
}
essential.ts
import { User } from "./user";
Here is additionally screenshot of exception :
,
You have a few choices:
Option 1: Use a module loader like Webpack, Browserify, and so forth.
Option 2: If you simply wish to compile *.ts to *.js with none module imports or exports, set compilerOptions.module to “none” in your tsconfig.json. Note that you just received’t have the ability to export/import modules if you set compilerOptions.module to “none”.
with this config your output is just one js file what will be uglified wunderfull, containing all referenced recordsdata in the principle.ts. i simply don’t know if ~/ works or if it’s important to set the trail relative to the config file, i’m not working with linux.
class User {
firstName: string;
lastName: string;
}
Main.ts:
/// <reference path="User.ts" />
// import { User } from "./user"; // not wanted if referenced
console.log(new User());
all reference declarations should be written at the start of the file
,
TypeScript by default makes use of node module decision. Node.js / CommonJS makes use of exports key phrase. However, CommonJS doesn’t run in browser and not using a module loader or module bundler. Hence, you want browserify or webpack to get it working in browser.
the Parcel bundler will create a dist listing containing all of your wanted recordsdata (html and transcoded JavaScript) and it’ll run a neighborhood webserver at:
class User {
firstName: string;
lastName: string;
}
essential.ts
/// <reference path="User.ts" />
// import { User } from "./user"; // not wanted if referenced & User.js file is loaded
console.log(new User());
,
This was my story:
I had the identical downside as a result of the browser makes use of fashionable export/import assertion, whereas Typescript makes use of the default commonJS. So, within the tsconfig.json file, set the next modifications:
goal: "es2015",
module: "es2015",
I feel in case you use BabolonJS,it might be higher to set "moduleResolution” to "node" explicitly, nonetheless it might make no distinction.