Yes you can!
Hugo uses Babel to compile JavaScript and through Babel you can also compile TypeScript. If you haven't already, you'll need to install some Babel packages for Hugo to use:
@babel/core @babel/cli @babel/preset-env
You'll also need Babel's TypeScript package:
@babel/preset-typescript
Setup your Babel config file (Hugo looks for babel.config.js, but you can change that if you'd like):
module.exports = {
"presets": [
"@babel/preset-typescript",
"@babel/preset-env",
],
}
Now Hugo can successfully compile TypeScript via Babel with the babel pipe:
{{ $typescript := resources.Get "scripts/main.ts" | babel }}
The problem is that Hugo will save this as 'scripts/index.ts' in your public directory. So, to fix this, you have to change the file during the process. One way is to take advantage of Hugo's asset bundling and essentially bundle itself as a new file:
{{ $javascript := slice $typescript | resources.Concat "scripts/main.js" }}
Finally, you can use that resource:
<script src='{{ $javascript.RelPermalink }}'></script>
<!-- or -->
<script>{{ $javascript.Content | safeJS }}</script>
Edit
There is one caveat: since Babel is not bundler and Hugo's bundler doesn't seem to support JavaScript modules, you can't import modules and have the same expected outcome as a bundler like Webpack.
It does look like, however, that this is currently a proposal for Hugo https://github.com/gohugoio/hugo/issues/7290.