TypeScriptCompiler icon indicating copy to clipboard operation
TypeScriptCompiler copied to clipboard

Support for Imports

Open syrusakbary opened this issue 3 years ago • 14 comments

It would be great, if Typescript projects that are using the import syntax to import variables, functions, classes or types could be compiled with tsc.

I tried the following:

import x from "./x";

And then tried to compile it, but it failed with "unknown statement type":

➜  tsc git:(main) ✗ ../__build/tsc-ninja/bin/tsc ../example.ts --emit=jit --nogc
unknown statement type

Thoughts?

syrusakbary avatar Jan 04 '22 12:01 syrusakbary

"Import" is not working for now, it is planned feature for future after generics. But can't say when and how I will implement it.

ASDAlexander77 avatar Jan 04 '22 12:01 ASDAlexander77

Would be amazing to have imports support. Everybody uses imports but not everybody uses generics :).

coderaiser avatar Jan 09 '22 20:01 coderaiser

it can be most difficult part of the project. Because it is generated into obj files you can use "export" and "declare" to split code into many files for now

ASDAlexander77 avatar Jan 11 '22 17:01 ASDAlexander77

added "/// reference" support in the last commit

ASDAlexander77 avatar Jan 18 '22 01:01 ASDAlexander77

Hey @ASDAlexander77 any suggestion on how to use the mentioned /// reference support?

Eager to start using it!

syrusakbary avatar Feb 14 '22 22:02 syrusakbary

check the code in TypeScriptCompiler\docs\how\cmake_vulkan\src\ but keep it in mind that compiler is in progress and requires a lot of debugging etc

ODuzhar avatar Feb 16 '22 09:02 ODuzhar

HI!, is import completed ? will you support import from so in the future?

lixiangsheng2018 avatar Mar 29 '22 13:03 lixiangsheng2018

yes, work is in progress, not sure to which level I will develop it, or how long will it take, but for now I am implementing very basic functionality for "import" (kind of /// )

ASDAlexander77 avatar Mar 29 '22 15:03 ASDAlexander77

last coomit contains simple implementation of "import" example:

/// <reference path="window.win32.d.ts" />

import "./appwindow";

export class Application {
    static appWindow: AppWindow;

    export static run() {
        this.appWindow = new AppWindow();
    }
}

ASDAlexander77 avatar Mar 31 '22 20:03 ASDAlexander77

That's an awesome first step @ASDAlexander77. Do you know if importing types or variables by name would work? Is the /// <reference ...> tag always required?

syrusakbary avatar Apr 01 '22 02:04 syrusakbary

Is the /// <reference ...> tag always required?

no, you don't need it if you don't use anything from the referenced file

ASDAlexander77 avatar Apr 01 '22 08:04 ASDAlexander77

it seems I still need to improve it not to use "/// <reference ...>"

ASDAlexander77 avatar Apr 01 '22 08:04 ASDAlexander77

That's an awesome first step @ASDAlexander77. Do you know if importing types or variables by name would work? Is the /// <reference ...> tag always required?

you don't need to use <reference ...> in the last commit

ASDAlexander77 avatar Apr 03 '22 00:04 ASDAlexander77

Do you know if importing types or variables by name would work?

not at this moment. and I am not sure if I need to impalement it, as it is not "JVM" and import working differently

ASDAlexander77 avatar Apr 03 '22 00:04 ASDAlexander77

"Import" is not working for now, it is planned feature for future after generics. But can't say when and how I will implement it.

Would be great to see, Then we could slap some socket libs to it and roll an HTTP server out ez : ))

If I may? I'd recommend making the import process similar to how

#pragma link library

works, allowing us to include C++ libs on demand too if possible? as there are existing tools available c++ to javascript (like emscripten) so producing types shouldn't be an issue.

This way we might get language interoperability at some extent?

azarus avatar May 14 '23 14:05 azarus

I think I will do import the way how C++ include DLL files

ASDAlexander77 avatar May 14 '23 20:05 ASDAlexander77

I think also a possibility for implementing imports is to bundle it using existing bundlers such as esbuild and then just compile?

I can't see why this wouldn't work if bundlers would generate/output TS files.

cunev avatar Jun 09 '23 14:06 cunev

esbuild

well, when u compile every file you get OBJs files, then "Link" will join them into 1 file. Seems what esbuild does.

ASDAlexander77 avatar Jun 12 '23 08:06 ASDAlexander77

added additional way to import share libraries

ASDAlexander77 avatar Jul 06 '23 19:07 ASDAlexander77

added additional way to import share libraries

Try import get the error

:0: error: redefinition of symbol named '__decls' :0: note: see existing symbol definition here .\src\use_shared.ts:1:1: error: module verification error import Service from "./shared"; ^

cereschen avatar Sep 05 '23 05:09 cereschen

Can u share your source code and compilation steps?

ASDAlexander77 avatar Sep 05 '23 06:09 ASDAlexander77

Can u share your source code and compilation steps?

// shared.ts
export default class Service {
}
// use_shared.ts
import Service from "./shared";

class MyComponent {
    constructor(public Service: Service) {
    }

    method(x: this) {
    }
}

function main()
{
	const myComponent = new MyComponent(null);
	print("done.");
}

run D:\tsc\tsc.exe --opt --emit=jit --nogc .\src\use_shared.ts

os: win10 version: v0.0-pre-alpha40

cereschen avatar Sep 05 '23 09:09 cereschen

you need to use latest from main branch. I am planning to release new build after improving "wasm" support

ASDAlexander77 avatar Sep 05 '23 17:09 ASDAlexander77