-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
[Design] Syntax to represent module namespace object in dynamic import #14844
Comments
Why not just like? var p = import<typeof MyModule1 | typeof MyModule2>(somecondition ? "./MyModule1" : "./MyModule2"); |
@cevek two things with that syntax: 1) it won't be clear that the output is a I will update the proposal with what we discuss during design meeting |
So looking at the proposed snippets above: import * as MyModule1 from "./MyModule1";
import * as MyModule12 from "./MyModule2";
var p = import(somecondition ? "./MyModule1" : "./MyModule2") as Promise<typeof MyModule1 | typeof MyModule2>; var p = import(somecondition ? "./MyModule1" : "./MyModule2") as Promise<moduleof MyModule1 | moduleof MyModule2>; I don't see how My hope is that this is some huge typo and that the |
@rozzzly it is a typo for this one.... Copy and paste mistake here 😭 I have updated the original post. |
Update
// 0.ts
export interface foo {}
export class C {}
// 1.ts
var p: module("./0");
If we go with (2.) then for dynamic import, to express the shape of import module will be var d = import(blah) as Promise<typeof module("blah")>; // very verbose Another syntax that comes up for using with dynamic import is var d = import<"blah">(blah);
var d = import(<"blah">blah); In both casting syntax, type argument must be string literal., Conclusion
var d = import<"blah">(blah);
var d = import(<"blah">blah);
|
This is a subset of this larger issue #13231 |
I definitely like the type operator for looking up modules, since it would probably be nice if declare function import<T extends string>(path: T): Promise<typeof module(T)>; where Oh, and this is unrelated but oddly unsatisfying: The import spec as currently written allows something like this: export default (x) => import(x); But not this: export default import; since it's not really a function. |
Unfortunately we can not do the general type operator; given the way the compiler is architected today all file-system interactions happen at the very beginning when we are collecting files to compile; whereas resolving types happens at a later stage where new files are not expected to be added, nor are file-system operations expected. so it has to be |
wouldn't the e.g. declare function foo(a: boolean): string;
type Q = typeof foo(true); // Q is string |
What is the final syntax? A module instance is required to avoid repeating the same I came across where there is a conditional import to only add hardware specific module, load the module to get an initialization side effect and doing some cleanup before exit. Currently I keep a variable of required function within the module instead of the module itself. In my opinion
var p: typeof module("a");
p = import<typeof module("a")>(a);
type moduleA = typeof module("a");
I prefer to avoid |
This seems like something that this feature request of mine could solve quite simply... TL;DR: it'd reify types as pseudo-properties (transparent to the runtime) and allow them to be passed around and defined like so. Support for this would fall out fairly naturally. However, it might potentially be a little cumbersome due to the later binding of types. |
Discussed this in #22445, and conclusion is to go with |
Sorry if I'm missing something, but what is wrong with this, which is already possible? import * as _foo from './foo'
let foo: typeof _foo Also want to point to this piece from the DT "common mistakes" readme why
|
@felixfbecker The string isn't a type parameter. |
@isiahmeadows I am aware - like
|
@felixfbecker why would you not just cast there? |
@weswigham that's my point |
This seems to not work with foo.js
Fails:
If I change foo.js to use EDIT: Nevermind. I have to use |
Another half of dynamic import #14774 is to enable users to be able to describe the shape of the module namespace object return by dynamic import. This is needed because
TypeScript currently can only resolve module if the specifier is a string literal (recall syntax of dynamic import
import(specifier)
) andPromise<any>
will be returned...Therefore we need a way for users to be able to specify the module shape in type argument of the Promise and escape noImplicitAny (Note if dynamic import is used as expression statement. we won't issue noImplicitAny)When we emit declaration file, we need a syntax to indicate that this is a
Promise of some module
. Currently compiler will give an error as reference external module is not the same as the module of containing file (or lack of one)🚲 🏠 There are two ares which need to be discuss:
Proposes for (1) - bring in the module (This assume that we will use casting syntax but it doesn't have)
moduleof
....when collecting module reference we will also collect frommoduleof
as wellProposes for (2) - indicate the return type
Just use casting
allow all of the above.
Design meeting update #14853
In addition to be able to simply refer to shape of module namespace object, it is also desirable to use as a
QualifiedName
(e.gvar x: <some syntax to indicate that we refer to module object>.namespace.interface
) and to have syntax that can easily allow users to be able to do so is crucial.Candidates we are currently considered:
I will want to discuss some performance/incremental parsing related to this area and see if we think it will be desirable to consider top-level type import like
import type * as ns from "./a.js".
The text was updated successfully, but these errors were encountered: