-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
eval() do not support TypeScript. #8256
Comments
this would probably require overriding the native eval to first detect if its typescript, if so, then start up the entirety of tsc, compile the javascript, ensure there's no errors, then give it to v8 to actually run it. This would incur a huge performance loss because tsc is relatively slow. I don't see benefits in ever allowing this because of that. |
You could add an option |
This would also not work correctly for type checking because eval can grab variables from its scope but the tsc checker does not know about the outer context and eval is invoked at runtime |
And it would require a lot of time to find a way to make it work I suppose? |
You would have to know what variables are declared in the scope, so either plug into the tsc type checker api, which is complicated, or get it from v8 somehow, also complicated. Overall i don't think the drawbacks outweight the pros:
|
Okay, it's too bad... |
@Ayfri If you wanted to use import/export, this is a step forwards: const mod = await tseval(`
import { red } from "https://deno.land/[email protected]/fmt/colors.ts";
export function echo(message: string) {
return red(message)
}
`)
console.log(mod.echo('hello'))
async function tseval(code: string) {
const moduleURL = import.meta.url.replace('file://', '') + '$eval.ts'
await Deno.writeFile(moduleURL, new TextEncoder().encode(code))
return import(moduleURL)
} |
Oh, that is neat! |
I'm getting an error : Error: The syntax of the file, directory, or volume name is incorrect. (os error 123)
at processResponse (deno:core/core.js:223:11)
at Object.jsonOpAsync (deno:core/core.js:240:12)
at async open (deno:runtime/js/30_files.js:44:17)
at async Object.writeFile (deno:runtime/js/40_write_file.js:54:18)
at async tseval (mod.ts:5:2) |
If you want to use that code from a remote module, you will need to change the moduleURL to a writable path. But when #5059 is released, it will get easier: function tseval (code: string) {
return import('data:application/javascript,' + encodeURIComponent(code));
} |
You can't use any typescript features in the native
eval()
function, you can't useimport/export
also, it would be great to be able to use these.The text was updated successfully, but these errors were encountered: