diff --git a/README.md b/README.md index e37225a..14bbf83 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,23 @@ task('build', async ctx => { }) ``` +And, if using TypeScript, add types to your options through the `task` generic: + +```ts +import { task, desc, option, strict } from 'foy' + +type BuildOptions = { + watch: boolean +} + +desc('Build ts files with tsc') +option('-w, --watch', 'watch file changes') +strict() // This will throw an error if you passed some options that doesn't defined via `option()` +task('build', async ctx => { // ctx.options now has type BuildOptions instead of unknown + await ctx.exec(`tsc ${ctx.options.watch ? '-w' : ''}`) +}) +``` + ```sh foy build -w ``` diff --git a/docs/index.html b/docs/index.html index 9de120a..35b83cd 100644 --- a/docs/index.html +++ b/docs/index.html @@ -90,6 +90,19 @@

Write a Foyfile

task('build', async ctx => { await ctx.exec(`tsc ${ctx.options.watch ? '-w' : ''}`) }) +

And, if using TypeScript, add types to your options through the task generic:

+
import { task, desc, option, strict } from 'foy'
+
+type BuildOptions = {
+  watch: boolean
+}
+
+desc('Build ts files with tsc')
+option('-w, --watch', 'watch file changes')
+strict() // This will throw an error if you passed some options that doesn't defined via `option()`
+task<BuildOptions>('build', async ctx => { // ctx.options now has type BuildOptions instead of unknown
+  await ctx.exec(`tsc ${ctx.options.watch ? '-w' : ''}`)
+})
foy build -w

Warning! If you want to set flags like strict for all tasks, please use setGlobalOptions:

import { setGlobalOptions } from 'foy'