-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support type-only/uninstantiated namespaces (#32)
This PR adds support for type-only/uninstantiated namespaces. i.e. namespaces that only use `type` and `interface`. ```ts export class C {} export namespace C { export type T = string; } ``` **Before:** Error. only `declare namespace ...` was allowed ❌ **Now:** Ok. namespace declaration erased ✅ --- The following cases remain unchanged: ```ts // Instantiated namespace errors namespace A { export let x = 1 } // error namespace B { ; } // error // Ambient namespace is erased declare namespace C { export let x = 1 } // ok erased declare module D { export let x = 1 } // ok erased // `module`-keyword errors module E { export let x = 1 } // error module F { export type x = number } // error ``` ## Testing Unit tests for both supported cases and unsupported cases (errors) have been added. ## Context This addition was motivated by [`--erasableSyntaxOnly`](microsoft/TypeScript#61011) and the recognition that in today's TypeScript, the only way to augment a class with types after the initial declaration is via namespaces. Whilst that use case can be solved using `declare namespace` that comes with [a hazard](https://github.com/bloomberg/ts-blank-space/blob/main/docs/unsupported_syntax.md#the-declare--hazard). The combination of `--erasableSyntaxOnly` checks and transforming uninstantiated namespaces provides a safer option. Thanks to @jakebailey for brining this to our attention microsoft/TypeScript#61011 (comment) --------- Signed-off-by: Ashley Claymore <[email protected]> Co-authored-by: Rob Palmer <[email protected]>
- Loading branch information
Showing
7 changed files
with
224 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
namespace Empty {} | ||
// ^^^^^^^^^^^^^^^ empty namespace | ||
|
||
namespace TypeOnly { | ||
type A = string; | ||
|
||
export type B = A | number; | ||
|
||
export interface I {} | ||
|
||
export namespace Inner { | ||
export type C = B; | ||
} | ||
} | ||
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type-only namespace | ||
|
||
namespace My.Internal.Types { | ||
export type Foo = number; | ||
} | ||
|
||
namespace With.Imports { | ||
import Types = My.Internal.Types; | ||
export type Foo = Types.Foo; | ||
} | ||
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ nested namespaces | ||
|
||
// declaring the existence of a runtime namespace: | ||
declare namespace Declared { | ||
export function foo(): void | ||
} | ||
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `declare namespace` | ||
Declared.foo(); // May throw at runtime if declaration was false | ||
|
||
export const x: With.Imports.Foo = 1; | ||
// ^^^^^^^^^^^^^^^^^^ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.