-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use d.ts specified types within a JavaScript Module Itself #14342
Comments
I also tried adding |
A |
@mhegazy - while technically, I would agree that it's WAD/WAI, from pragmatic perspective of smooth and incremental transition of JS-only projects to TS, it would be greatly beneficial to be able to define the types and definitions in separate non-production files and utilize the additional knowledge for VSCode/Intellisense, validation, etc. And today - most of it works (as @mjbvz mentioned), except the module JS file itself (e.g. Again, not challenging "working as intended", but asking for considering extension of the "intended" for practical reasons of JS projects that cannot immediately just switch to TS. |
The mapping from a .d.ts to a .js file is not straight forward. There are may patterns in JS that can be used to achieve some of the ES6 declarations. e.g. a class. similarly a module shape in node is not the same as it is in ES6 (and .d.ts). So given all of that, if you are the author of this .d.ts, consider using JSDoc to annotate your .js file. |
Here is a thought (which may be more on vscode side rather than ts): |
I could understand the Mohamed's implicated complexity of all possible JS structures in a module (beyond what I asked for) may make it a big project by itself, thus rejected as "too much work and expecting too much bugs and bughancements coming afterwards". Unless it's worth it considering the number of existing JS projects that cannot move yet (for whatever technical or political reasons). Enough said - I'll shut up now. |
I have a team member that currently prefers not to switch to using typescript due to not wanting a build step. I do enjoy typescript and its associated tooling. I thought that a good compromise would be to use declaration files, but they do not work in their associated JS modules. I would like to use declaration files in their associated javascript file which would very helpful in refactorings and adding features and other maintenance tasks. Here is a (small) example project repo: https://gist.github.com/kaleb/e2f33c5f8ff3bc046a8b430ac8440c27 |
A file with a top-level for this you have two options,
// person.d.ts
declare global {
type PersonStruct = PersonStruct;
}
|
@mhegazy this is not an understanding about globals. This is a feature request to have a typescript declaration module declare types for a corresponding javascript module inside the module, not creating separate global types. I would prefer to write actual typescript, but I cannot currently. Another option would be to use JSDoc type annotations, but there are a few missing features in typescript JSDoc handling such as the |
I understand.. but module scopes have an existing meaning.. merging two module scopes is not something we have done before, and not sure how to reason about that. scopes is a well defied concept for most software developers, and having two files share the same scope is unexpected to say the least. |
I think the better way to do this is to allow you to define a new module, say |
From microsoft/vscode#21454
TypeScript Version: 2.2.1
Code
For a javascript project
a.js
:a.d.ts
:b.js
Expected behavior:
Within both
a.js
andb.js
, the type ofabc
is(number, number) => boolean
Actual behavior:
Within
a.js
, the type ofabc
is(any, any) => void
Within
b.js
, the type ofabc
is(number, number) => boolean
The
d.ts
file is not applied within a module itself, only when it is imported into other filesThe text was updated successfully, but these errors were encountered: