I'm new to Node.js and I'm learning some basics now. I'm trying to use some typescript code to convert into .js code later.
I wrote this simple code to test
import * as fs from 'fs'
const argv = require('yargs')
.alias('f', 'filename')
.alias('c', 'content')
.demandOption('filename')
.demandOption('content')
.argv
fs.writeFile(argv.filename, argv.content, (error)=>{
if(error)
throw error
console.log(`File ${argv.filename} saved.`)
})
And this works fine. But when I change the line require('yargs') to import, like below:
import * as fs from 'fs'
import * as yargs from 'yargs'
const argv = yargs
.alias('f', 'filename')
.alias('c', 'content')
.demandOption('filename')
.demandOption('content')
.argv
fs.writeFile(argv.filename, argv.content, (error)=>{
if(error)
throw error
console.log(`File ${argv.filename} saved.`)
})
I'm getting this error:
Argument of type 'unknown' is not assignable to parameter of type 'string | number | Buffer | URL'.
Type '{}' is missing the following properties from type 'URL': hash, host, hostname, href, and 9 more.ts(2345)
Does anybody know what's the difference between using module/import that is causing this error? For fs library both ways work fine in this example.