-
Notifications
You must be signed in to change notification settings - Fork 262
Reduce duplicated effort for module+global/script typings #402
Comments
Doesn't TypeScript 2.0 UMD support fix this specific issue? |
Yeah, just need to refresh my memory and connecting the dots. |
Workaround by @basarat UPDATE: However, I think this won't work if there are multiple references to the typings. e.g. you and one of your dependencies both reference it. |
@unional, thanks for the link to the workaround by @basarat. I'm afraid I don't quite follow how this approach could be applied to a type definition. If it's not too much trouble, would you be able to illustrate how this could be applied in the case of say iScroll or Knockout? I'd really like to understand this better. |
I need to build an example to test the statement that I think it might not work. The workaround is exposing them through two files, |
Modules stay modulesBasically if a file is declared as a module, there is no way to move it into the global namespace without thinking too hard: declare module "foo" {
export var foo: any;
} Any attempt to use this import * as foo from "foo"; That would essentially make the file a module and therefore cannot pollute the global namespace. Hack to move it into the globalThe one linked here : microsoft/TypeScript#7352 (comment) works by pollute the global namespace seperately: interface My { // To introduce the name `My` to the global namespace
} And then augment it from the module: import foo = require('foo');
declare global {
interface My {
user: typeof foo;
}
} etc. Note this is a very bad workaround for decreasing code duplication as we need two files ( Long term fixUMD module declarations : microsoft/TypeScript#7125 now allow you to create a declaration file that is a module: export function doThing(): string;
export function doTheOtherThing(): void;
export as namespace myLib; That can be imported as a module: import * as foo from "./foo"; or can be imported globally: /// <reference path="my-lib.d.ts" />
myLib.doThing(); So we actually don't need any duplication effort on our part. The effort moves into the hands of the library consumer (either global or as a module). Hope that helps 🌹 |
@basarat, do you think that if it is being referenced multiple times, would it cause duplicate identifier error? That's what I want to test about. 🌹 |
Thanks @basarat - I need to let this sink in a bit but I think I follow you. So it's not really worth advising people to use the hack; just wait for UMD to ship. (Which looking at the talks from Build is probably only about 2 months or so) 🌷 |
@unional are you asking about |
Hack. |
@unional Not if its referenced multiple times pointing to the same file. However if its across different files its effectively the following and will result in an error: declare global {
interface Array<T> {
foo:number; // Duplicate ident error
}
}
declare global {
interface Array<T> {
foo:number; // Duplicate ident error
}
} |
Great. If that is the case, then it might be possible that we can start doing it today in typings. Since they are ambient, the dependencies aren't followed not inlined. Therefore it should (crossing fingers) not cause problem at the end consumer. Thanks for your great hack! 🌹 |
...nor inlined.... |
Just tried it. Having a bit of issue. ping @blakeembrey . The problem I have is when I run typings ERR! message Attempted to compile "iscroll" as an ambient module, but it looks like an external module.
typings ERR! cwd /Users/hwong/github/typed-typings/typed-iscroll/global/iscroll/out
typings ERR! system Darwin 14.5.0
typings ERR! command "/usr/local/bin/node" "/usr/local/bin/typings" "bundle" "-o" "out" "--ambient"
typings ERR! node -v v4.3.2
typings ERR! typings -v 0.7.12
typings ERR! If you need help, you may report this error at:
typings ERR! <https://github.com/typings/typings/issues> The message is right that indeed |
@unional What's the expected behaviour? |
@blakeembrey how to make it work? 😛 When used, it needs to be in two separate files. Maybe // main.d.ts
/// <reference path="main/ambient/iscroll/index.d.ts" />
/// <reference path="main/ambient/iscroll/indexAugment.d.ts" /> But on the other hand, it may not worthwhile because it could be too complicate to implement for a hack that will go away when |
Don't you just bundle it without |
Without typings bundle -o out
typings ERR! message Unable to compile "iscroll", the typings are meant to be installed as ambient but attempted to be compiled as an external module
typings ERR! cwd /Users/hwong/github/typed-typings/typed-iscroll/global/iscroll
typings ERR! system Darwin 14.5.0
typings ERR! command "/usr/local/bin/node" "/usr/local/bin/typings" "bundle" "-o" "out"
typings ERR! node -v v4.3.2
typings ERR! typings -v 0.7.12
typings ERR! If you need help, you may report this error at:
typings ERR! <https://github.com/typings/typings/issues> |
Bundle the definition itself? |
What do you mean? // typings.json
{
"name": "iscroll",
"main": "iscroll.d.ts",
"ambient": true,
"files": [
"iscroll.d.ts",
"iscrollAugment.d.ts"
],
"version": "5.1.3",
"homepage": "https://github.com/cubiq/iscroll",
"devDependencies": {
"blue-tape": "registry:npm/blue-tape#0.1.0+20160322235613"
},
"ambientDevDependencies": {
"node": "registry:dt/node#4.0.0+20160319033040"
}
} |
I don't know what to say, can you provide a more isolated example? Looking at https://github.com/typed-typings/typed-iscroll/blob/master/global/iscroll/typings.json#L6-L7 you are trying to merge an ambient and external version which won't work. Why don't you make it only external versions? |
Create an issue on |
Maybe it's worth just waiting for 2.0? If it's only a couple of months away then this might be more trouble than it's worth. Also you might get people in the future looking at Typings with the hack in place and then thinking the hack is good practice. |
💯 I am sure this wasn't intended as a part of |
😄 maybe we can change topic on how to get We still want to have a separate file for UMD. But now it will be in top-level import/export format. So we need to detect it is UMD and make it an exception to the current validation. |
FWIW the hack is simpler now as you no longer need a separate file for introducing the name into the global namespace: microsoft/TypeScript#7161 (comment) 🌹 I still can't make the choice of what is the right way to do it though, and I leave that to your brilliant hands 🌹 |
Just think of an idea as I'm looking into fixing some issue on env-atom:
@blakeembrey what do you think? |
@unional That could work. I think if it works it's good. You may want to tweak the bundle command to omit dependencies just in case, but seems reasonable to do it this way and end up with just one type of bundle. |
Currently when we create typings that can be consumed as module AND global/script, we need to write it twice.
e.g.:
https://github.com/typed-contrib/knockout/blob/master/index.d.ts
https://github.com/typed-contrib/knockout/blob/master/global/index.d.ts
and
https://github.com/typed-typings/typed-iscroll/blob/master/iscroll.d.ts
https://github.com/typed-typings/typed-iscroll/blob/master/iscroll/iscroll.d.ts
This is due to TypeScript does not allow this:
or similar construct.
Is there an issue opened on https://github.com/Microsoft/TypeScript can be referenced and tracked here?
The text was updated successfully, but these errors were encountered: