-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
How to Compare Two Definitions Structurally? #7535
Comments
Just compile: import * as foo from "./foo";
import * as bar from "./bar";
let _foo: typeof foo;
let _bar: typeof bar;
_foo = _bar = _foo; I haven't tested but should work 🌹 |
@basarat Interesting hack. I guess I'd have to generate this file, assuming it works, to do the test. The only outstanding piece is |
The only restriction i see, there has to be a |
@blakeembrey can you log an issue to track allowing |
As for the structural comparison, who is doing this? is it a command-line tool? do you expect it to be part of the compiler? how would it be surfaced to the user? what is the intended result? if it is a separate tool, you can do what @basarat suggested. |
Absolutely.
This is for writing type definitions with package authors. Definitely a CLI tool. I don't expect it to be part of the compiler. It can be part of @typings to validate a type definition on top of a JavaScript project. The intended result is that new, breaking API changes in JS have a chance of being caught by CI. For example, implementing typings for Express.js - references: expressjs/express#2818 and typings/typings#322. |
Using the compiler API, we can definitely expose a new method on the TypeChecker to compare 2 types. so you would walk over the .d.ts file and the .js file simultaneously and get type for each export, and then compare their types. |
@mhegazy That sounds like it'd be perfect 👍 |
Feel free to file an issue for the API changes as needed. |
@mhegazy I couldn't find an existing issue and it seems more realistic as this same request, but would the TypeScript compiler consider exposing a mode where it only type checks I was linked to a new project doing something like this, which renewed my interest in having a feature like this exposed (https://github.com/Raynos/jsig2). |
We will need a proposal for what that means to implement. also what are the scenarios you would use this mode. i would be inclined to say this is a tool that the compiler API can support. but I would like to understand the full picture first. |
Anywhere we're working to merge |
how far would this go, like there are JS dynamic constructs that can not be verified statically, e.g. var mod = require("./other");
for(var i in mod) {
exports[i + "_extension"] = mod[i];
} how would you check this against a .d.ts file? |
I don't think there's any requirement to analyse that, but could it be allowed if there's extra exports in the Basically, I think it'd be amazing if we can have a JavaScript/TypeScript file and create one |
We should be able to create a .d.ts from a .js file. but i would think of this as a manual process, like you create the .d.ts to get you started. but then you would manually update the .d.ts file. |
You may want to take a look at TSEvolve which takes two |
Yes, I made that. Yes, it could be used. Currently the project uses TypeScript 1.5 to parse the .d.ts files, and it doesn't work with NodeJS libraries. (Updating to TypeScript 2.1 shouldn't take long, but getting it to work with NodeJS could be quite a hassle). |
I'm not sure if this feature is currently supported, or even whether the whole chain of features have been enabled to make this workflow work, but at the lowest level how would one compare two definitions (
1.d.ts
and2.d.ts
) and check if they're compatible? On top of this, would it be possible to access the definitions for a JavaScript library?Reasoning: It'd be great to be able to type existing JavaScript libraries, but the biggest thing hindering third-parties is being able to integrate the workflow with CI. One way to improve this workflow would be with the
--allowJs
flag on the library itself to generate one definition, then comparing it to the user-written definition. Having the declarations flag work with--allowJs
would also allow users to get started writing definitions 10x faster - just runtsc entry.js --allowJs --declaration --out definitions
and improve the types little by little.The text was updated successfully, but these errors were encountered: