Typescript autocomplete puts errors on require calls
Describe the bug
After running dnscontrol write-types I noticed that the block of require calls in my main dnscontrol.js file was marked with errors in vscode:
When I mouse over the marked text I see this error message:
I expect this is fallout from re-using the require command from javascript as part of the dnscontrol DSL.
To Reproduce Steps to reproduce the behavior:
- Install dnscontrol
- Follow instructions to install the typescript autocomplete files and activate them in a dnscontro.js file
- Add a
require("file")call to your dnscontro.js - See the incorrect error text
A clear and concise description of what you expected to happen:
The typescript autocomplete should handle the require top-level command properly, not through an inappropriate error message.
require is a special keyword; using the require function causes typescript to infer that this file is a module, thus losing the ability to infer global variables.
If you need this functionality, here's a temporary solution:
var require2 = require;
require2("./zones/example.com.js");
In addition, you can create a tsconfig.json file that hints the typescript with correct runtime, which has better inferencing capabilities.
My tsconfig.json
{
"compilerOptions": {
"lib": ["es5"],
"allowJs": true,
"checkJs": true,
"module": "None",
"strict": true,
"noEmit": true,
},
"include": [
"zones/**/*.js",
"dnsconfig.js",
"types-dnscontrol.d.ts",
]
}
I'm aware require is a special keyword, which is why I mentioned the bit about it being a javascript command.
I tried putting your tsconfig.json file in the root dir of my dnscontrol setup and then loaded dnscontrol.js into vscode but it is still showing the incorrect error. I am not a javascript/typescript programmer so I'm just banging around here. How does one use a tsconfig.json file?
just a reference for you.
my-domains
├── .gitignore
├── .gitlab-ci.yml
├── creds.json
├── dnsconfig.js
├── spfcache.json
├── tsconfig.json
├── types-dnscontrol.d.ts
└── zones
├── example.com.js
├── other...
tsconfig.json should include all your js/ts code.
...
"include": [
"zones/**/*.js", // <- all zones
"dnsconfig.js", // <- entry point
"types-dnscontrol.d.ts", // <- types file
]
}
dnsconfig.js
var REG_NONE = NewRegistrar("none");
var DSP_CLOUDFLARE = NewDnsProvider("cloudflare");
require_glob("./zones/", false);
// or require a single file manually
// var require2 = require;
// require2("./zones/example.com.js");
example.com.js
D("example.com", REG_NONE,
DnsProvider(DSP_CLOUDFLARE),
TXT("@", "example"),
);
And as it turns out, the types-dnscontrol.d.ts info for require_glob incorrectly demands the second argument, recursive, which can be left off to take its default value as well. I discovered that while working through your example.
I'm not an expert but I think its a matter of editing documentation/language-reference/top-level-functions/require_glob.md to mark that as optional. that is, change recursive: boolean to recursive: boolean?
To test:
go generate
go build
dnscontrol write-types
Test the new types-dnscontrol.d.ts file.
Closed by https://github.com/StackExchange/dnscontrol/pull/3508.
@cafferata The fix for require_glob doesn't fix the mistaken typescript error for the require call. Did you close this because that can't be fixed?
Re-opening.