2016-06-18 84 views
0

我正在用Typescript進行一些測試,甚至當我強制將錯誤代碼編譯爲javascript時。我正在使用vscode任務進行編譯。即使出現錯誤也能進行Typescript編譯TS2304

app.ts

function getAllBooks() { 
    let books = [ 
     { title: "book1", author: "author1", available: true }, 
     { title: "book2", author: "author2", available: false }, 
     { title: "book3", author: "author3", available: true } 
    ]; 

    return books; 
} 


function logFirstAvailable(books) { 
    let numberOfBooks = books.length; 

    for (let book of books) { 
     let firstAvailable = ""; 

     if (book.available) { 
      firstAvailable = book.title; 
      break; 
     } 
    } 

    console.log('Total books: ' + numberOfBooks); 
    console.log('First Available: ' + firstAvailable); // error TS2304: Cannot find name 'firstAvailable'. 
} 

const ALL_BOOKS = getAllBooks(); 
logFirstAvailable(ALL_BOOKS); 

app.js

function getAllBooks() { 
    var books = [ 
     { title: "book1", author: "author1", available: true }, 
     { title: "book2", author: "author2", available: false }, 
     { title: "book3", author: "author3", available: true } 
    ]; 
    return books; 
} 
function logFirstAvailable(books) { 
    var numberOfBooks = books.length; 
    for (var _i = 0, books_1 = books; _i < books_1.length; _i++) { 
     var book = books_1[_i]; 
     var firstAvailable = ""; 
     if (book.available) { 
      firstAvailable = book.title; 
      break; 
     } 
    } 
    console.log('Total books: ' + numberOfBooks); 
    console.log('First Available: ' + firstAvailable); 
} 
var ALL_BOOKS = getAllBooks(); 
logFirstAvailable(ALL_BOOKS); 
//# sourceMappingURL=app.js.map 

tasks.json

{ 
    "version": "0.1.0", 
    "command": "tsc", 
    "isShellCommand": true, 
    "args": [ 
     "--target", "ES5", 
     "--outdir", "js", 
     "--sourceMap", 
     "--watch", 
     "app.ts" 
    ], 
    "showOutput": "silent", 
    "problemMatcher": "$tsc" 
} 

tsconfig.json

{ 
    "compilerOptions": { 
     "module": "commonjs" 
    } 
} 

如果這是一個正常的行爲,如何阻止打字稿如果發生任何錯誤編譯?

回答

1

你試過--noEmitOnError?閱讀tsc --help下列出的參數聽起來就像是票。

{ 
    "version": "0.1.0", 
    "command": "tsc", 
    "isShellCommand": true, 
    "args": [ 
     "--target", "ES5", 
     "--outdir", "js", 
     "--sourceMap", 
     "--noEmitOnError", 
     "--watch", 
     "app.ts" 
    ], 
    "showOutput": "silent", 
    "problemMatcher": "$tsc" 
} 
+0

謝謝,工作正常! –

相關問題