Skip to content

Compiler API transpile type cast code wrong. #16935

Description

@saibing

TypeScript Version: 2.3.4

Code

  • compiler api invoke
import * as ts from "typescript";                                                                                                                                                       
function _go_transpileModule(script: string): string {
    let result = ts.transpileModule(script, {compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES5}}); 
    return result.outputText;
}  

I call _go_transpileModule method in myself embed v8 engine application, not node.js.

  • ts source code
function sqlDemo() {                                           
                                                               
    try {                                                      
        let s = db.sql();                                      
        let records = <Object[]>s.exec('select * from script');
                                                               
        records.forEach((r) => printRecord(r));                
                                                               
        for (let i = 0; i < records.length; i++) {             
            printRecord(records[i]);                           
        }                                                      
                                                               
        s.exec("update script set id = 1111 where id = 1;");   
    } catch (error) {                                          
        console.log(error.name, error.message);                
    }                                                          
}                       
  • the javascript code after transpiled
function sqlDemo() {
    try {
        var s = db.sql();
        var records = <Object />, _a =  > s.exec('select * from script');
        records.forEach(function (r) { return printRecord(r); });
        for (var i = 0; i < records.length; i++) {
            printRecord(records[i]);
        }
        s.exec("update script set id = 1111 where id = 1;");
    }
    catch (error) {
        console.log(error.name, error.message);
    }
}

Expected behavior:

everything is ok.

Actual behavior:

but the follow code:

<Object[]>

has been translated to

<Object />, _a =  >

How can I solve this problem?

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions