I'm using a lot of libraries both my own and 3rd party. I see the "typings" directory contains some for Jquery and WinRT… but how are they created?
Typescript – How to produce a .d.ts “typings” definition file from an existing JavaScript library
tsctypescript
Related Solutions
This issue was mentioned for a migration from drools 3.06 to 4.0.7, so what version of eclipse and drools are you using?
This might be related to a classpath issue:
Using the debugger I realized that the Drools
PackageBuilder
tried to load the classes from the
Thread.currentThread().getContextClassLoader();
This
ClassLoader
does not contain my agent classes! Even the system class loader does not contain my classes.
The solution was:
Instead of creating plain
PackageBuilder
andRuleBase
instances, one has to create them with aPackageBuilderConfiguration
and aRuleBaseConfiguration
both with the currentclassLoader
configured:
ClassLoader classLoader = this.getClass().getClassLoader();
PackageBuilderConfiguration configuration = new PackageBuilderConfiguration();
configuration.setClassLoader(classLoader);
PackageBuilder builder = new PackageBuilder(configuration);
builder.addPackageFromDrl(source);
RuleBaseConfiguration ruleBaseConfiguration = new RuleBaseConfiguration();
ruleBaseConfiguration.setClassLoader(classLoader);
ruleBase = RuleBaseFactory.newRuleBase(ruleBaseConfiguration);
ruleBase.addPackage(builder.getPackage());
This is actually a nice mathematical problem. Assume:
int side = to - from + 1; /// the size/width of the square.
the value at any point in the square (row, col) is:
from + ((row + col) % side)
you should be able to put that in your loops and "smoke it".
Edit based on comment asking for explanation.
The trick is to loop through all the positions in the 'matrix'. Given that the matrix is square, the loops are relatively simple, just two loops (nested) that traverse the system:
final int side = to - from + 1;
for (int row = 0; row < side; row++) {
for(int col = 0; col < side; col++) {
... magic goes here....
}
}
Now, in this loop, we have the variables row
and col
which represent the cell in the matrix we are interested in. The value in that cell needs to be proportional to the distance it is from the origin..... let me explain.... If the origin is the top left (which it is), then the distances from the origin are:
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
The distance is the sum of the row and the column...... (rows and columns start counting from 0).
The values we put in each matrix are limited to a fixed range. For the above example, with a square of size 5, it could have been specified as printSquare(1,5)
.
The value in each cell is the from value (1 in this example) plus the distance from the origin... naively, this would look like:
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
here the values in the cell have exceeded the limit of 5, and we need to wrap them around... so, the trick is to 'wrap' the distances from the origin..... and the 'modulo' operator is great for that. First, consider the original 'origin distance' matrix:
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
if we instead populate this matrix with 'the remainder of the distance when dividing by 5' (the modulo 5, or %5) we get the matrix:
0 1 2 3 4
1 2 3 4 0
2 3 4 0 1
3 4 0 1 2
4 0 1 2 3
Now, if we add this 'modulo' result to the from value (1), we get our final matrix:
1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4
in a sense, all you need to know is that the value at each cell is:
the from value plus the remainder when you divide the 'distance' by the width.
Here's the code I tested with:
public static final String buildSquare(final int from, final int to) {
final StringBuilder sb = new StringBuilder(side * side);
final int side = to - from + 1;
for (int row = 0; row < side; row++) {
for(int col = 0; col < side; col++) {
sb.append( from + ((row + col) % side) );
}
sb.append("\n");
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println(buildSquare(1, 5));
System.out.println(buildSquare(3, 9));
System.out.println(buildSquare(5, 5));
System.out.println(buildSquare(0, 9));
System.out.println(buildSquare(0, 3));
}
Best Answer
There are a few options available for you depending on the library in question, how it's written, and what level of accuracy you're looking for. Let's review the options, in roughly descending order of desirability.
Maybe It Exists Already
Always check DefinitelyTyped (https://github.com/DefinitelyTyped/DefinitelyTyped) first. This is a community repo full of literally thousands of .d.ts files and it's very likely the thing you're using is already there. You should also check TypeSearch (https://microsoft.github.io/TypeSearch/) which is a search engine for NPM-published .d.ts files; this will have slightly more definitions than DefinitelyTyped. A few modules are also shipping their own definitions as part of their NPM distribution, so also see if that's the case before trying to write your own.
Maybe You Don't Need One
TypeScript now supports the
--allowJs
flag and will make more JS-based inferences in .js files. You can try including the .js file in your compilation along with the--allowJs
setting to see if this gives you good enough type information. TypeScript will recognize things like ES5-style classes and JSDoc comments in these files, but may get tripped up if the library initializes itself in a weird way.Get Started With
--allowJs
If
--allowJs
gave you decent results and you want to write a better definition file yourself, you can combine--allowJs
with--declaration
to see TypeScript's "best guess" at the types of the library. This will give you a decent starting point, and may be as good as a hand-authored file if the JSDoc comments are well-written and the compiler was able to find them.Get Started with dts-gen
If
--allowJs
didn't work, you might want to use dts-gen (https://github.com/Microsoft/dts-gen) to get a starting point. This tool uses the runtime shape of the object to accurately enumerate all available properties. On the plus side this tends to be very accurate, but the tool does not yet support scraping the JSDoc comments to populate additional types. You run this like so:This will generate
your-module.d.ts
in the current folder.Hit the Snooze Button
If you just want to do it all later and go without types for a while, in TypeScript 2.0 you can now write
which will let you
import
the"foo"
module with typeany
. If you have a global you want to deal with later, just writewhich will give you a
foo
variable.