我找到了可能的答案:
Is any JavaScript code a valid TypeScript code?.
我正在使用Visual Studio和TypeScript插件进行Web项目.在以下帖子中,我找到了这个答案:
Not any valid JavaScript code is valid TypeScript see my example below.
var testVar = 4; testVar = "asdf";
TypeScript gives the following
error: Cannot convert string to number. To make that work in
TypeScript add “:any” like below.
var testVar: any = 4; testVar = "asdf"
This happens because TypeScript
noticed testVar is declared and in the declaration it is assigned a
number and therefore it decides it should stay a number.
我正在尝试使用HighCharts.如果我将代码保存在Javascript文件中,则不会出现任何错误.但是,当我将代码保存在Typescript文件中时,出现错误:
“Cannot find name “HighCharts”.
在这里,您可以看到我的代码的相关部分:
// Put definitions of highcharts, highstocks below this line
// Example 1 Chart
var initializeChart1 = function () {
require(['jquery', 'highcharts', 'highchartsMore', 'highchartsExporting'], function ($) {
$("#myChart").highcharts({
chart: {
type: 'spline',
backgroundColor: '#ECECEC',
// ------THIS GIVES AN ERROR, BUT WORKING WHEN SAVED AS .JS file
animation: Highcharts.svg, // don't animate in old IE
// -------------------------
...
我想将我的JavaScript代码保留在TS文件中的原因很简单:我目前正在学习TypeScript,并且我将JavaScript和TypeScript版本或相同的代码保留在同一文件中.根据TypeScript是否完成/正常工作,我将其取消注释并注释JavaScript.
我的问题是:
>为什么会出现此错误?
>是否可以使用任何JavaScript代码
在TS文件中?