我最近偶然发现了TypeScript中这种奇怪的(imo)行为.在编译期间,只有当接口没有必填字段时,如果预期变量的类型是接口,它才会抱怨多余的属性.链接到TypeScript Playground#1:http://goo.gl/rnsLjd
interface IAnimal {
name?: string;
}
class Animal implements IAnimal {
}
var x : IAnimal = { bar: true }; // Object literal may only specify known properties, and 'bar' does not exist in type 'IAnimal'
var y : Animal = { bar: true }; // Just fine.. why?
function foo<T>(t: T) {
}
foo<IAnimal>({ bar: true }); // Object literal may only specify known properties, and 'bar' does not exist in type 'IAnimal' …Run Code Online (Sandbox Code Playgroud) 我有简单的javascript警告框我希望它在5秒后自动关闭.如果可能请帮忙.
<script type="text/javascript">
function show_alert()
{
alert("Hello! I am an alert box!");
}
</script>
<input type="button" onclick="show_alert()" value="Show alert box" />
Run Code Online (Sandbox Code Playgroud)