- Superset of JavaScript created by Microsoft
- Allows static strict typing
- Extra features - interfaces, enums, tuples, generics
- Supports modern features (arrow functions, let, const)
- Based on the .NET harmony specification
- Not suitable for large application
- Lacks strong typing
- Weired inheritance, unfamiliar syntax
- Only errors during runtime
- Suffers type coercion
Install TypeScript globally or locally in your project. It's recommended to install it locally within your project to avoid version conflicts. Run
npm install typescript --save-devTypeScript requires a configuration file to specify compiler options. In your project's root directory, create a tsconfig.json file and copy-paste this full code.Or copy any tsconfig.json from typescript environment.
{
"compilerOptions": {
"target": "es2016",
/* Modules */
"module": "commonjs", /* Specify what module code is generated. */
"rootDir": "./resources/assets/ts", /* Specify the root folder within your source files. */
"outDir": "./public/js/compiled_from_ts", /* Specify an output folder for all emitted files. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
/* Type Checking */
"strict": true, /* Enable all strict type-checking options. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}
Create your TypeScript files in the specified directory (e.g., resources/assets). You can organize your files as needed and write your frontend logic in TypeScript.
file invoice.ts
let countryName:string = "Bangladesh";
console.log(countryName);tsc <your_file.ts>or compiled all .ts files automatically (recommended)
tsc -wafter command this, then goto public/js/compiled_from_ts. You'll see a invoice.js file created automatically.
Once you have the compiled JavaScript files, you can include them in your Blade views like any other JavaScript file:
@push('scripts')
<script src="{{ asset('js/compiled_from_ts/invoice.js') }}"></script>
// ... your other js code or file.
@endpushJust include this in your expected file.
if you need jQuery then run the command -
npm install @types/jquery --save-devimport jquery in the .ts file
import * as $ from 'jquery';You can then use
$(function() {
console.log(12);
}); But if you've already imported jquery file in your layout, then you can ignore this jQuery import part