bonzaiferroni / Traveler

Traveler - A general movement solution for Screeps.com
GNU General Public License v3.0
116 stars 38 forks source link

reliance on "magical interface" #6

Open bonzaiferroni opened 7 years ago

bonzaiferroni commented 7 years ago

There was a discussion in #typescript about the use of declaration files and we came to the conclusion that putting all the typings in index.d.ts is not very good practice (source). These ought to be moved back to Traveler.ts except for the creep.travelTo() declaration, which was the original reason for having index.d.ts.

jmgilman commented 7 years ago

The source also states:

Also, you should not be using ambient declaration files (e.g. d.ts files) for code that you are writing

It's ambiguous, but would creep.travelTo() count as code that you are writing? If so, even those declarations should be moved to a .ts file.

bonzaiferroni commented 7 years ago

Extending the game prototypes is a sticky issue and I'm not aware of a solution that would easily let you do it other than a .d.ts. If there is another way that is better practice, I'm all ears.

RiftLurker commented 7 years ago

There were a lot of discussions going on over ambient declaration files and extending screeps declarations. Most people seemed to be writing theiir declaration files manually, but I never feelt that it was the correct approach. My main issue with it is that these declarations are omitted when actually generating declarations and I'm somewhat reliefed someone else has the same issue now. The way I ended up doing it is by adding the following to the file modifying the prototype:

declare global {
    interface Creep {
        foo(): void;
    }
}
bonzaiferroni commented 7 years ago

Interesting, I like that it wouldn't require a separate .d.ts file that every codebase will have to handle according to its unique tsconfig settings. Traveler.ts can remain just a single file.

kotarou commented 7 years ago

I can see cases where having a traveler.d.ts is useful, for external use (or linking via typings).

But in general, yes, "magical interfaces" in code you are writing - for yourself - should be avoided.