|
| 1 | +# TypeScript |
| 2 | + |
| 3 | +- [Basic Usage](#basic-usage) |
| 4 | +- [Example TSConfig](#example-tsconfig) |
| 5 | +- [Usage With Vue](#usage-with-vue) |
| 6 | + |
| 7 | +### Basic Usage |
| 8 | + |
| 9 | +```js |
| 10 | +mix.ts('src/app.ts', 'dist'); |
| 11 | + |
| 12 | +// Alias |
| 13 | +mix.typeScript('src/app.ts', 'dist'); |
| 14 | +``` |
| 15 | + |
| 16 | +TypeScript support in Mix is built on top of the existing `mix.js()` feature set. In fact, it's as simple as updating your `mix.js()` calls to `mix.ts()`. Doing so will trigger the installation of all necessary TypeScript packages, as well as an update to the generated webpack configuration to add the necessary `ts-loader` webpack loader. |
| 17 | + |
| 18 | +### Example TSConfig |
| 19 | + |
| 20 | +Of course, you'll still want to make any appropriate TypeScript-specific tweaks, such as creating a `tsconfig.json` file in your project root, and potentially installing [DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyTyped). Here's an example `tsconfig.json` file to get you started. |
| 21 | + |
| 22 | +```json |
| 23 | +// tsconfig.json |
| 24 | +{ |
| 25 | + "compilerOptions": { |
| 26 | + "target": "esnext", |
| 27 | + "module": "esnext", |
| 28 | + "allowJs": true, |
| 29 | + "strict": true, |
| 30 | + "esModuleInterop": true |
| 31 | + }, |
| 32 | + "include": ["resources/js"] |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +[Refer here](https://www.typescriptlang.org/tsconfig/) for a full list of supported options. |
| 37 | + |
| 38 | +### Usage With Vue |
| 39 | + |
| 40 | +Enabling TypeScript compilation within your Vue single file components is a cinch. Begin by updating your `webpack.mix.js` file to specify that you wish to compile TypeScript while also enabling support for Vue single file components. |
| 41 | + |
| 42 | +```js |
| 43 | +mix.ts('resources/js/app.js', 'public/js').vue(); |
| 44 | +``` |
| 45 | + |
| 46 | +Next, update your single file component(s) to allow TypeScript to infer types. |
| 47 | + |
| 48 | +##### Using Vue 2 |
| 49 | + |
| 50 | +```vue |
| 51 | +<script lang="ts"> |
| 52 | + // resources/js/components/greet.vue |
| 53 | +
|
| 54 | + // use Vue.extend to enable type inference |
| 55 | + export default Vue.extend({ |
| 56 | + data () { |
| 57 | + return { |
| 58 | + message: 'Hello' |
| 59 | + } |
| 60 | + }, |
| 61 | +
|
| 62 | + computed: { |
| 63 | + greeting(): string { |
| 64 | + return this.greet() + '!' |
| 65 | + } |
| 66 | + }, |
| 67 | +
|
| 68 | + methods: { |
| 69 | + greet(): string { |
| 70 | + return this.message + ' world' |
| 71 | + } |
| 72 | + } |
| 73 | + }); |
| 74 | +</script> |
| 75 | +``` |
| 76 | + |
| 77 | +##### Using Vue 3 |
| 78 | + |
| 79 | +```vue |
| 80 | +<script lang="ts"> |
| 81 | + // resources/js/components/greet.vue |
| 82 | +
|
| 83 | + import { defineComponent } from "vue"; |
| 84 | +
|
| 85 | + // use defineComponent to enable type inference |
| 86 | + export default defineComponent({ |
| 87 | + data () { |
| 88 | + return { |
| 89 | + message: 'Hello' |
| 90 | + } |
| 91 | + }, |
| 92 | +
|
| 93 | + computed: { |
| 94 | + greeting(): string { |
| 95 | + return this.greet() + '!' |
| 96 | + } |
| 97 | + }, |
| 98 | +
|
| 99 | + methods: { |
| 100 | + greet(): string { |
| 101 | + return this.message + ' world' |
| 102 | + } |
| 103 | + } |
| 104 | + }); |
| 105 | +</script> |
| 106 | +``` |
| 107 | + |
| 108 | +And that should do it! |
0 commit comments