试试playground:
typeof a == "object"
将导致
运算符'=='无法应用于类型'“string”| “数字”| “符号”| “对象”| “function”'和'“array”'。)
这个错误消息不仅对我毫无意义(a instanceof Array
也没关系),它似乎也是最近的变化。您现在必须使用<div id="jsGrid"></div>
<script>
var id = "<?php echo $id?>" //the id being passed to the View.
var resources = [];
var instruments = [
{Name: "", Id: 0},
{Name: "Drum Kit", Id: 1},
{Name: "Guitar", Id: 2},
{Name: "Piano", Id: 3},
{Name: "Violin", Id: 4},
];
$("#jsGrid").jsGrid({
controller: {
loadData: function (filter) {
$.ajax({
type: "GET",
url: "<?= $host.$basepath ?>/instrument/",
data: filter
});
},
insertItem: function (item) {
$.ajax({
type: "POST",
url: "<?= $host.$basepath ?>/instrument/",
data: item
});
},
updateItem: function (item) {
$.ajax({
type: "PUT",
url: "<?= $host.$basepath ?>/instrument/",
data: item
});
},
deleteItem: function (item) {
$.ajax({
type: "DELETE",
url: "<?= $host.$basepath ?>/instrument/",
data: item
});
}
},
width: "100%",
height: "400px",
inserting: true,
editing: true,
sorting: true,
paging: true,
data: resources,
fields: [
{
name: "Instrument",
type: "select",
items: instruments,
valueField: "Id",
textField: "Name",
validate: "required",
width: 175
},
{name: "Comment", type: "text", width: 150},
{name: "resource_id", type: "text", width: 150, visible: false,
insertTemplate: function() {
var input = this.__proto__.insertTemplate.call(this);
input.val(id)
return input;
}},
{type: "control"}
]
});
</script>
</div>
,这是我上次检查时没有必要的。你能解释一下吗?
答案 0 :(得分:2)
正如SLaks所指出的,typeof运算符永远不会返回&#34; array&#34;。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof
它从来没有奏效,但TypeScript只是变得足够聪明,可以在2.0中警告你。
答案 1 :(得分:0)
以下是一个完整的工作示例:
var a: string | string[];
// You need this in the example to ensure the compiler won't determine that a
// is never a string if you set it to an array...
if (new Date().getDay() > 3) {
a = 'a string';
} else {
a = [
'an',
'array'
];
}
if (Array.isArray(a)) {
var example1 = a; // example1: string[]
} else {
var example2 = a; // example2: string
}
中间代码块仅用于演示目的,如果以var a: string | string[] = []
开始此示例,变量example2
实际上将最终为never
类型,因为编译器足够智能看到没有a
变量将被设置为除数组之外的任何内容的情况。