Skip to content

Ability to reference code as a declaration (ambient) #1753

@stanvass

Description

@stanvass

Feature proposal:

A new flag in <reference>:

/// <reference path="..." ambient="true" />

The effect of this flag would be to treat the referenced code as an ambient declaration, even if contains implementation code. The referenced code will be used for type-checking, autocompletion and all other needs by the compiler, but its code won't be included in the compiled output.

This means we don't have to compile separate .d.ts files for every .ts file when we use it in this context (the assumption is the referenced code is compiled and loaded via another channel, like a <script> tag in browsers).

Use case:

There is no lean way to separate code in modules for browser apps right now, without introducing external modules... and then using a third party solution to strip them away and compact them into JS bundles.

The redundancy and complexity of having to use two tools that mostly cancel each other out (adding modules and stripping away modules) can be avoided if we can refer to a code module as an internal module, but not include it in the produced output.

Let's say I have three browser apps on the same site. They all share a module called Lib, which is also written in TypeScript. I compile it separately and load it with a script tag. With the proposed feature, I can compile all my apps by using:

/// <reference path="Lib.ts" ambient="true" />

Without having to compile an explicit declaration for Lib.ts every time I modify it.

I tried to use the --declaration switch, but unfortunately it gives no control over where the .d.ts file is produced and how it's called (it just goes where the JS file goes). Even if it did, it'd be more cumbersome than a reference switch, because the declaration is already contained with a .ts source file, the only thing that's different is the use case. We don't need to have all those redundant files in order to fulfill the use case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already createdSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions