I'm trying to use export and import but it not working I get an error
Here is my code HTML :
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> @RenderBody() <script src="~/scripts/user.js"></script> <script src="~/scripts/main.js"></script> </body> </html>
User.ts :
export class User { firstName: string; lastName: string; }
main.ts
import { User } from "./user";
Here is also screenshot of exception :
Use a named export to export a type in TypeScript, e.g. export type Person = {} . The exported type can be imported by using a named import as import {Person} from './another-file' . You can have as many named exports as necessary in a single file.
To solve the "ReferenceError require is not defined" error, remove the type property if it's set to module in your package. json file and rename any files that have a . mjs extension to have a . js extension.
In TypeScript, just as in ECMAScript 2015, any file containing a top-level import or export is considered a module. Conversely, a file without any top-level import or export declarations is treated as a script whose contents are available in the global scope (and therefore to modules as well).
It's just JavaScript Modules and their associated keyword like import , export , and export default are JavaScript constructs, not typescript. However typescript added the exporting and importing of interfaces and type aliases to it.
You have a couple of options:
Option 1: Use a module loader like Webpack, Browserify, etc.
Option 2: If you just want to compile *.ts
to *.js
without any module imports or exports, set compilerOptions.module
to "none" in your tsconfig.json
. Note that you won't be able to export/import modules when you set compilerOptions.module
to "none".
If 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