-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow a module to implement an interface #420
Comments
How would this work with external modules? It's likely once people can use it for internal, they'll also want to use it with external. |
That's a good question. I don't know which syntax would be the best, but here are a few suggestions: implements Showable; // I would prefer this one.
module implements Showable;
export implements Showable; It should only be allowed on external modules that don't use an export assignment, since if you use an export assignment, the thing that you export can already have an |
Approved. We prefer the syntax
and agreed that this is unneeded for files |
Some more questions:
declare module "Module" implements Interface { }
import i : Interface = require("Module");
module Foo {
export interface IBar {
(a:string): void;
}
export module Bar implements IBar { // should this be an error?
export interface Interface {}
}
function Bar(a: string) : void { } // not exported
}
var bar: Foo.IBar = Foo.Bar; |
It should be allowed on ambient external modules. For these modules two syntaxes should be allowed in my opinion: declare module "first" implements Foo { }
declare module "second" {
interface Bar { }
export implements Bar; // this syntax is necessary, with the first syntax you can't reference Bar.
} Or should Bar be in the scope in an implements clause before the opening Adding type info to an import statement isn't really useful in my opinion, since you can add the type info to the module itself. And for merged declarations, I'd say that the module block that contains the implements clause should implement the interface. That also prevents issues with visibility. |
How would this be related to #2159? A namespace implements an interface? |
@jbondc If we had this, it would also apply to namespaces. You should think of internal modules and namespaces as isomorphic. |
Are you sure you want to go down an implementational path where "namespaces" can implement interfaces? |
Oh wow, this has been approved for quite a while. @RyanCavanaugh, @DanielRosenwasser, @mhegazy unless you have any second thoughts or tweaks, I'll probably implement this soonish. |
I withdraw my previous skepticism, I actually exited for the new structural possibilities it would bring. In line with that, please consider enforcing the interface of the aggregate of the interface instead of only the block that declares the implementation - The nature of namespaces/modules is to be spread out and to contain a lot of non-trivial components. I'd like to be able to use this, but I certainly don't want to define my whole namespace/module in the same file. Why not just use a class in that case? |
@Elephant-Vessel I'm not sure if we are talking about Modules, or Namespaces, or Packages, or Features, or... |
@aluanhaddad What do you mean? |
I mean that at the time that this discussion started module didn't mean what it means today. We now use the term namespace to refer to what is described in the OP as a module, while module has taken on a more precise and incompatible meaning. So when you talk about multiple files taking part in this implementation are you referring to namespaces or modules? |
I have found a sorta-workaround. interface IUtilities {
Version: string;
Build: number;
PrintVersion(): string;
}
module Utilities {
export var Version = "1.1";
export var Build = 23;
export function PrintVersion() {
return `v${Version}b${Build}`;
}
}
//by assigning it into a var of the interface type, we provoke TypeScript to check the module comply with the interface
var p: IUtilities = Utilities; |
@shaipetel thanks for sharing but this doesn’t support default exports? |
@EloB the problem with default exports, that you do not include inside a namespace or module - is that the point of import doesn't guarantee to import all members. So can't verify it against an interface, I guess (it is not all or nothing, in a sense of all members/functions or none).
I'm assuming in a nutshell, that's one of the problems. You can't guarantee the entire module will be consumed/exported and won't be broken up by the build. |
@shaipetel Typescript is orthogonal to tree-shaking, meaning that it will still Typecheck unused code paths. |
@reaktivo exactly, but that is what can cause the problem. Now in your code - TypeScript will tell you A1 and A2 both implement iA, right? So, assume you have a function (not inside your code, maybe a global one. example would be if you try to JSON.stringify your import) that expects iA as a parameter. TypeScript would basically tell you its ok to send either your imported A1 or A2, without knowing which members you chose to import, and which members are needed by your function, in during dev - TypeScript gives the green light assuming everything would be there. The build wouldn't know what that function expects, and how it is going to use the parameter - so tree shaking won't be able to identify the members that are needed (dependencies). I hope I'm making sense... |
So by changing tree shaken to follow module requirement (optional) would fix everything? |
@EloB disregarding tree shaking, the same could be done for normal modules using interface IUtilities {
Version: string;
Build: number;
PrintVersion(): string;
}
const Version = "1.1";
const Build = 23;
const PrintVersion = () => `v${Version}b${Build}`;
// by assigning it into a var of the interface type,
// we provoke TypeScript to check the module comply with the interface.
const module: IUtilities = {
Version, Build, PrintVersion,
};
export default module; |
@JarnoRFB Thanks for your time. This doesn't work tree shaking right? I already know that you can write like this but from my understanding it won't work with tree shaking? |
Yes, but also, we can't simply disregard tree shaking. |
@EloB my understanding is unfortunately to limited to comment on that. I just adapted your solution for normal modules. |
Put this at your module file and File: YourModule.ts /* -------------------------------------------------------------------------- */
/* Module Type Checking */
/* -------------------------------------------------------------------------- */
type _ = typeof import('./YourModule');
const __TYPE_CHECKING__: YourModuleInterface = {} as _;
/* ----------------------------------- --- ---------------------------------- */ Ugly? Yes, but it works. If your module does not There is no cyclical-dependency since the import is "garbage collected" and is not transpiled. EDIT: Here a more clear (semantical?) variable name example to avoid misinterpretations. /* myModule.ts */
/* ------------------------------- Type Check ------------------------------- */
type _myModule = typeof import('./myModule');
const _myModuleImplements: MyModuleInterface = {} as _myModule; |
@nthypes, thanks for posting this. I'm okay with ugly if it works. However, I must be missing something because this doesn't seem to work for me: /* commands.ts */
/* -------------------------------------------------------------------------- */
/* Module Type Checking */
/* -------------------------------------------------------------------------- */
interface YourModuleInterface {
foo: string;
}
type _ = typeof import('./commands');
export const foo: YourModuleInterface = "hello" as _;
/* ----------------------------------- --- ---------------------------------- */ I also tried this but this also doesn't work. /* commands.ts */
* -------------------------------------------------------------------------- */
/* Module Type Checking */
/* -------------------------------------------------------------------------- */
interface YourModuleInterface {
foo: string;
}
type _ = typeof import('./commands');
export const __TYPE_CHECKING__: YourModuleInterface = { foo: "hello" } as _;
/* ----------------------------------- --- ---------------------------------- */ |
@ericmasiello the implementation is wrong. You should not edit Should error (wrong type): /* commands.ts */
export interface YourModuleInterface {
foo: string;
}
export const foo = false
/* -------------------------------------------------------------------------- */
/* Module Type Checking */
/* -------------------------------------------------------------------------- */
type _ = typeof import('./commands');
const __TYPE_CHECKING__: YourModuleInterface = {} as _;
/* ----------------------------------- --- ---------------------------------- */ Should error (no export): /* commands.ts */
export interface YourModuleInterface {
foo: string;
}
const foo = false
/* -------------------------------------------------------------------------- */
/* Module Type Checking */
/* -------------------------------------------------------------------------- */
type _ = typeof import('./commands');
const __TYPE_CHECKING__: YourModuleInterface = {} as _;
/* ----------------------------------- --- ---------------------------------- */ Should pass: /* commands.ts */
export interface YourModuleInterface {
foo: string;
}
export const foo = 'false'
/* -------------------------------------------------------------------------- */
/* Module Type Checking */
/* -------------------------------------------------------------------------- */
type _ = typeof import('./commands');
const __TYPE_CHECKING__: YourModuleInterface = {} as _;
/* ----------------------------------- --- ---------------------------------- */ PS: I also added a more "semantical" version on my original post. |
Got it. Thanks, @nthypes. Adding on to this, the variable named /*
Inside the file commands.ts
@note we need to import the same file we're in (`commands.ts`) in the module type checking section below
*/
// this defines the interface for our module (commands.ts), i.e., what it must export
export interface CommandsModuleInterface {
// put whatever you want here for your use case.
// This use case says we must export a value named `foo` of the type `string`
foo: string;
}
// here, we implement our interface
export const foo = "hello";
/* -------------------------------------------------------------------------- */
/* Below this line is the "hack" to validate our command.ts module.
/* It works by importing the `type` of the module (file) we're in, i.e., `commands.ts`
/* -------------------------------------------------------------------------- */
// this infers the type of what we're *actually* exporting from `commands.ts`
// we store this as a type called `_` (again this is arbitrary)
type _ = typeof import('./commands');
// The line below here is where the actual type checking occurs.
// We assign an arbitrarily named `const` as `__MODULE_TYPE_CHECK__`
// and specify the type as our desired module interface, `CommandsModuleInterface`.
// We assign the `const` `CommandsModuleInterface` a value of `{}` but immediately
// try to type assert that`__MODULE_TYPE_CHECK__`, which we said should be of type
// `CommandsModuleInterface`, matches the type actually exported by our module
// and assigned the type `_`.
// @note The eslint-disable-next-line is optional. I needed it for my lint rules
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const __MODULE_TYPE_CHECK__: CommandsModuleInterface = {} as _;
/* ----------------------------------- --- ---------------------------------- */ |
Also if you want auto-completion with intellisense: const MODULE: MyModuleInterface = {
someProperty: ...,
someMethod(param) { }
}
export const someMethod = MODULE.someMethod or const MODULE: MyModuleInterface = {
someProperty: ...,
someMethod(param) { }
}
export const { someProperty, someMethod } = MODULE; |
This is great. I've amended the example slightly to not emit an empty runtime object: type THIS_MODULE = typeof import('./commands');
type TYPE_CHECK<T extends CommandsModuleInterface> = T;
declare const _: TYPE_CHECK<THIS_MODULE>; |
Too bad you have to hard-code the file name in the file itself. There is a high chance of it getting misaligned after renaming/copying. |
If this happens you will receive an type error. |
see also: microsoft/TypeScript#420 (comment) thanks: @nthypes @ericmasiello
UPDATE: I'm using a more generic implementation to reduce the amount of boilerplate per file. For those interested: /* satisfies.ts https://github.com/microsoft/TypeScript/issues/420 */
/* eslint-disable */
/* -------------------------------------------------------------------------- */
/* Module Type Checking */
/* -------------------------------------------------------------------------- */
// This is a hack to make sure that the module type is correct
/**
* # Example
* ```typescript UserManagement.ts
*
* import { satisfies } from 'satisfies'
*
* interface IUserManagement {
* addUser: () => boolean
* }
*
* export const addUser = () => true
*
* satisfies<IUserManagement, typeof import('./test')>;
* ```
*/
export function satisfies<U, T extends U>() {
return 0 ?? (void 0 as T);
} /* UserManagement.ts */
import { satisfies } from 'satisfies'
interface IUserManagement {
addUser: () => boolean
}
export const addUser = () => true
satisfies<IUserManagement, typeof import("./UserManagement.ts")>; // very smal runtime footprint - only import statment Of course, you can change it to a name that makes the most sense to you: |
This is a pretty old issue, but so relevant! Would be nice to have this feature in TypeScript :D I created the issue #58029, but since it's the same subject, I'll give my 2 cents here: My proposal is adding a way to define a module interface, and a way to use it, adding the 📃 Motivating Example// file user-module-interface.ts
export module interface UserModuleInterface {
async function getUser(id: string): Promise<User>
async function createUser(): Promise<void>
}
// ---------------
// file prisma-user.ts
import type { UserModuleInterface } from "./user-module-interface"
module implements UserModuleInterface
export async function getUser(id) {}
export async function createUser() {}
// ---------------
// another-file.ts
import { getUser, createUser } from "./prisma-user" Explaining the exampleFirst thing we have to do is creating a Everything inside a Then you can import (as a // file prisma-user.ts
import type { UserModuleInterface } from "./user-module-interface"
module implements UserModuleInterface {
export async function getUser(id) {}
export async function createUser() {}
} We can also implement more than one interface, like in classes: // file article-module-interface.ts
export module interface ArticleModuleInterface {
function getArticles(): Promise<Article[]>
}
// ---------------
// file prisma-user.ts
import type { UserModuleInterface } from "./user-module-interface"
import type { ArticleModuleInterface } from "./artictle-module-interface"
module implements UserModuleInterface, ArticleModuleInterface
export async function getUser(id) {}
export async function createUser() {}
export async function getArticles() {} RulesThe module that is using Shouldn't be a problem to implement other functions that are not defined in a We can possibly make some functions optional (not required to this suggestion): // file user-module-interface.ts
export module interface UserModuleInterface {
async function getUser(id: string): Promise<User>
// createUser is optional
async function createUser?(): Promise<void>
}
// ---------------
// file prisma-user.ts
import type { UserModuleInterface } from "./user-module-interface"
module implements UserModuleInterface
export async function getUser(id) {}
// no errors, even if `createUser` is not implemented 💻 Use CasesIn this proposal, we do not rely on runtime features. All types ( The functions defined inside a module that extends a And with this implementation, we have tree-shaking, because we can import only the functions we'll use. To do something similar today, we have to create a |
@fdaciuk Definitely think that modules implementing interfaces / types would warrant a special syntax to do away with the clunky workarounds presented in the thread. However, why would we need a special |
I don't think we need @JarnoRFB. Maybe just a simple |
@DanielRosenwasser this issue is 10yo. Do you think there is any chance of this being implemented in TypeScript soon? |
Maybe @RyanCavanaugh can help answer this =) |
I'd like to bump this for another use case: if you have a {
"exports": {
"node": "./file.node.ts",
"require": "./file.require.ts",
"module": "./file.module.ts",
"default": "./file.default.ts"
}
} And you want to ensure that all those 4 files are exposing the same interface for the whole module (and not that 1 forgot to expose a function, or implemented a different type) |
It would be useful when a module can implement an interface using the
implements
keyword. Syntax:module MyModule implements MyInterface { ... }
.Example:
The text was updated successfully, but these errors were encountered: