diff --git a/poelstra3poc/README.md b/poelstra3poc/README.md new file mode 100644 index 0000000..73538bd --- /dev/null +++ b/poelstra3poc/README.md @@ -0,0 +1,23 @@ +This tree shows an almost fully working example of having a mix of native and non-Typescript packages. + +It is a direct copy of `poelstra3`, and is a proof-of-concept of how external module resolution would work if +https://github.com/Microsoft/TypeScript/issues/2338 and https://github.com/Microsoft/TypeScript/issues/2839 is implemented. + +It has the following relevant changes compared to `poelstra3`: +* `import ... = require("...");` statements were rewritten from e.g. `myutils` to its 'resolved' equivalent `../typings/myutils/index` + +It compiles! (using tsc 1.5.0-alpha) + +In contrast to `poelstra2poc`, this one doesn't run in practice, because of: +``` +$ node ../dist/myprogram.js + +module.js:340 + throw err; + ^ +Error: Cannot find module '../typings/myutils/index' +``` + +That's to be expected, because we made `tsc` happy by resolving to the typings, but that's not where the `.js` lives. + +However, hovering over the `a` and `b` vars in `myprogram.ts` shows correct types! diff --git a/poelstra3poc/dist/myprogram.d.ts b/poelstra3poc/dist/myprogram.d.ts new file mode 100644 index 0000000..e69de29 diff --git a/poelstra3poc/dist/myprogram.js b/poelstra3poc/dist/myprogram.js new file mode 100644 index 0000000..5e9d398 --- /dev/null +++ b/poelstra3poc/dist/myprogram.js @@ -0,0 +1,6 @@ +var mylib = require("../node_modules/mylib/dist/index"); +var myotherlib = require('../node_modules/myotherlib/dist/index'); +var a = mylib.myfunc(); +var b = myotherlib.myotherfunc(); +console.log(typeof a.foo); +console.log(typeof b.foo); diff --git a/poelstra3poc/node_modules/mylib/dist/index.d.ts b/poelstra3poc/node_modules/mylib/dist/index.d.ts new file mode 100644 index 0000000..d102804 --- /dev/null +++ b/poelstra3poc/node_modules/mylib/dist/index.d.ts @@ -0,0 +1,2 @@ +import myutils = require("../typings/myutils/index"); +export declare function myfunc(): myutils.SomeOldInterface; diff --git a/poelstra3poc/node_modules/mylib/dist/index.js b/poelstra3poc/node_modules/mylib/dist/index.js new file mode 100644 index 0000000..3f6ac60 --- /dev/null +++ b/poelstra3poc/node_modules/mylib/dist/index.js @@ -0,0 +1,5 @@ +var myutils = require("../typings/myutils/index"); +function myfunc() { + return myutils.something(); +} +exports.myfunc = myfunc; diff --git a/poelstra3poc/node_modules/mylib/node_modules/myutils/dist/index.js b/poelstra3poc/node_modules/mylib/node_modules/myutils/dist/index.js new file mode 100644 index 0000000..58c7f4f --- /dev/null +++ b/poelstra3poc/node_modules/mylib/node_modules/myutils/dist/index.js @@ -0,0 +1,4 @@ +function something() { + return { foo: '1' }; +} +exports.something = something; diff --git a/poelstra3poc/node_modules/mylib/node_modules/myutils/package.json b/poelstra3poc/node_modules/mylib/node_modules/myutils/package.json new file mode 100644 index 0000000..d9e4915 --- /dev/null +++ b/poelstra3poc/node_modules/mylib/node_modules/myutils/package.json @@ -0,0 +1,5 @@ +{ + "name": "myutils", + "version": "1.0.0", + "main": "./dist/index.js" +} diff --git a/poelstra3poc/node_modules/mylib/package.json b/poelstra3poc/node_modules/mylib/package.json new file mode 100644 index 0000000..b83a284 --- /dev/null +++ b/poelstra3poc/node_modules/mylib/package.json @@ -0,0 +1,6 @@ +{ + "name": "mylib", + "version": "1.0.0", + "main": "./dist/index.js", + "typings": "./dist/index.d.ts", "//": "Should not be needed if we have https://github.com/Microsoft/TypeScript/issues/2338#issuecomment-94555288" +} diff --git a/poelstra3poc/node_modules/mylib/ts/index.ts b/poelstra3poc/node_modules/mylib/ts/index.ts new file mode 100644 index 0000000..73b5aa5 --- /dev/null +++ b/poelstra3poc/node_modules/mylib/ts/index.ts @@ -0,0 +1,4 @@ +import myutils = require("../typings/myutils/index"); +export function myfunc(): myutils.SomeOldInterface { + return myutils.something(); +} diff --git a/poelstra3poc/node_modules/mylib/ts/tsconfig.json b/poelstra3poc/node_modules/mylib/ts/tsconfig.json new file mode 100644 index 0000000..99b7b01 --- /dev/null +++ b/poelstra3poc/node_modules/mylib/ts/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "outDir": "../dist", + "declaration": true + }, + "filesGlob": [ + "./**/*.ts", + "!./node_modules/**/*.ts" + ], + "files": [ + "./index.ts" + ] +} diff --git a/poelstra3poc/node_modules/mylib/typings/myutils/index.d.ts b/poelstra3poc/node_modules/mylib/typings/myutils/index.d.ts new file mode 100644 index 0000000..b75f272 --- /dev/null +++ b/poelstra3poc/node_modules/mylib/typings/myutils/index.d.ts @@ -0,0 +1,7 @@ +// Typing for myutils v1.0 +// Note: it's a typing as Typescript would generate (it did, actually): no `declare module "..." { }`, no `/// ` + +export interface SomeOldInterface { + foo: string; +} +export declare function something(): SomeOldInterface; diff --git a/poelstra3poc/node_modules/myotherlib/dist/index.d.ts b/poelstra3poc/node_modules/myotherlib/dist/index.d.ts new file mode 100644 index 0000000..e6e898d --- /dev/null +++ b/poelstra3poc/node_modules/myotherlib/dist/index.d.ts @@ -0,0 +1,2 @@ +import myutils = require("../typings/myutils/index"); +export declare function myotherfunc(): myutils.SomeNewerInterface; diff --git a/poelstra3poc/node_modules/myotherlib/dist/index.js b/poelstra3poc/node_modules/myotherlib/dist/index.js new file mode 100644 index 0000000..64cd8ac --- /dev/null +++ b/poelstra3poc/node_modules/myotherlib/dist/index.js @@ -0,0 +1,5 @@ +var myutils = require("../typings/myutils/index"); +function myotherfunc() { + return myutils.something(); +} +exports.myotherfunc = myotherfunc; diff --git a/poelstra3poc/node_modules/myotherlib/node_modules/myutils/dist/index.js b/poelstra3poc/node_modules/myotherlib/node_modules/myutils/dist/index.js new file mode 100644 index 0000000..b451a9c --- /dev/null +++ b/poelstra3poc/node_modules/myotherlib/node_modules/myutils/dist/index.js @@ -0,0 +1,4 @@ +function something() { + return { foo: 1 }; +} +exports.something = something; diff --git a/poelstra3poc/node_modules/myotherlib/node_modules/myutils/package.json b/poelstra3poc/node_modules/myotherlib/node_modules/myutils/package.json new file mode 100644 index 0000000..e66c067 --- /dev/null +++ b/poelstra3poc/node_modules/myotherlib/node_modules/myutils/package.json @@ -0,0 +1,5 @@ +{ + "name": "myutils", + "version": "2.0.0", + "main": "./dist/index.js" +} diff --git a/poelstra3poc/node_modules/myotherlib/package.json b/poelstra3poc/node_modules/myotherlib/package.json new file mode 100644 index 0000000..3b36953 --- /dev/null +++ b/poelstra3poc/node_modules/myotherlib/package.json @@ -0,0 +1,6 @@ +{ + "name": "myotherlib", + "version": "1.0.0", + "main": "./dist/index.js", + "typings": "./dist/index.d.ts", "//": "Should not be needed if we have https://github.com/Microsoft/TypeScript/issues/2338#issuecomment-94555288" +} diff --git a/poelstra3poc/node_modules/myotherlib/ts/index.ts b/poelstra3poc/node_modules/myotherlib/ts/index.ts new file mode 100644 index 0000000..e61adee --- /dev/null +++ b/poelstra3poc/node_modules/myotherlib/ts/index.ts @@ -0,0 +1,4 @@ +import myutils = require("../typings/myutils/index"); +export function myotherfunc() { + return myutils.something(); +} diff --git a/poelstra3poc/node_modules/myotherlib/ts/tsconfig.json b/poelstra3poc/node_modules/myotherlib/ts/tsconfig.json new file mode 100644 index 0000000..99b7b01 --- /dev/null +++ b/poelstra3poc/node_modules/myotherlib/ts/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "outDir": "../dist", + "declaration": true + }, + "filesGlob": [ + "./**/*.ts", + "!./node_modules/**/*.ts" + ], + "files": [ + "./index.ts" + ] +} diff --git a/poelstra3poc/node_modules/myotherlib/typings/myutils/index.d.ts b/poelstra3poc/node_modules/myotherlib/typings/myutils/index.d.ts new file mode 100644 index 0000000..f7a3448 --- /dev/null +++ b/poelstra3poc/node_modules/myotherlib/typings/myutils/index.d.ts @@ -0,0 +1,7 @@ +// Typing for myutils v2.0 +// Note: it's a typing as Typescript would generate (it did, actually): no `declare module "..." { }`, no `/// ` + +export interface SomeNewerInterface { + foo: number; +} +export declare function something(): SomeNewerInterface; diff --git a/poelstra3poc/ts/myprogram.ts b/poelstra3poc/ts/myprogram.ts new file mode 100644 index 0000000..79aeb09 --- /dev/null +++ b/poelstra3poc/ts/myprogram.ts @@ -0,0 +1,8 @@ +import mylib = require("../node_modules/mylib/dist/index"); +import myotherlib = require('../node_modules/myotherlib/dist/index'); + +var a = mylib.myfunc(); +var b = myotherlib.myotherfunc(); + +console.log(typeof a.foo); // string +console.log(typeof b.foo); // number diff --git a/poelstra3poc/ts/tsconfig.json b/poelstra3poc/ts/tsconfig.json new file mode 100644 index 0000000..dbac419 --- /dev/null +++ b/poelstra3poc/ts/tsconfig.json @@ -0,0 +1,19 @@ +{ + "version": "1.4.1", + "compilerOptions": { + "target": "es5", + "module": "commonjs", + "noImplicitAny": false, + "removeComments": true, + "noLib": false, + "outDir": "../dist", + "declaration": true + }, + "filesGlob": [ + "./**/*.ts", + "!./node_modules/**/*.ts" + ], + "files": [ + "./myprogram.ts" + ] +}