我知道并不完全是最新的,因为没有提到type和abstract关键字。例如,以下内容现在是有效的TypeScript:
interface A {
b: string;
}
// I can't see anywhere in documentation this is mentioned.
type C = A;
var d: C;
d.b = 'something';
除了解析源代码或阅读有关GitHub问题的所有最新更改之外,还有更好的地方来更新文档吗?
在typescript中,我可以像这样导入另一个module\命名空间:
namespace Shapes2 {
import shapes = Shapes;
var bar = new shapes.Bar();
}
但是,我可以很容易地直接引用名称空间。
namespace Shapes3{
var shapes = Shapes;
var bar = new shapes.Bar();
}
import做了什么有用的事情吗?
什么时候我想要输入import而不是var
我有一个type声明如下:
type Position = {
white: number[];
black: number[];
}
当我分析该项目时,我会看到以下错误:
error Use an interface instead of a type literal @typescript-eslint/prefer-interface
有关导致错误的规则的文档如下:
接口通常优于类型文字,因为接口可以实现、扩展和合并。
这个规则在TSLint和ESLint之间很常见。我知道interface比type更强大,但是当我不需要interface优势和type就足够
据我所知,TypeScript允许为任何类型创建别名。例如:
type MyNum = number;
var y: MyNum;
y = 33; // it's ok
但以下代码无效:
type MyType = Object;
const dt = new MyType(); // here is an error: 'MyType only refers to a type, but is being used as a value here'
我错在哪里,如何创建MyType实例?
我有使用对象作为参数的函数,如下所示:
interface Options {
foo?: string;
bar?: number;
};
function fooNction(opts: Options): void {
}
这在某些情况下工作得很好,但不是全部:
fooNction({foo: "s"}); // OK
fooNction({a: "x"}); // fine as TS gives an Error as expected
fooNction("hello"); // no Erro
我想要创建一个反应组件,包装路由组件,以创建受保护的路由。我需要获得用户通常在<Route>元素上指定的<Route>属性,以及由我定义的属性、feature、role和fallback,然后每个其他属性都应该从包装器组件传递到路由组件。
问题是在TypeScript中强输入这些道具。我尝试过几种不同的方法,但我遇到了不同的错误。例如,如果我尝试下面的示例,就会得到RouteProps上的错误:“接口只能扩展对象类型或对象类型与静态已知成员的交集”。我怎样才能做到这一点?
import React from 'react'
import {
Inde