在TypeScript + React项目中,我使用的是react-dnd及其DefinitelyTyped类型:
interface ExampleScreenProps { a, b, c }
interface ExampleScreenState { x, y, z }
class ExampleScreen extends React.Component<ExampleScreenProps, ExampleScreenState> { }
export default DragDropContext(HTML5Backend)(ExampleScreen);
Run Code Online (Sandbox Code Playgroud)
这将在另一个组件中呈现:
import ExampleScreen from "./ExampleScreen";
<ExampleScreen a="a" b="b" c="c" />
Run Code Online (Sandbox Code Playgroud)
这适用于TS 1.8,没有任何错误.当我升级到TS 2.0时,我得到以下编译错误:
错误:(90,10)TS2600:JSX元素属性类型'(ExampleScreenProps&{children?:ReactNode;})| (ExampleScreenProps&{children ...'可能不是联合类型.
这是以下类型的定义DragDropContext:
export function DragDropContext<P>(
backend: Backend
): <P>(componentClass: React.ComponentClass<P> | React.StatelessComponent<P>) => ContextComponentClass<P>;
Run Code Online (Sandbox Code Playgroud)
我不能把它放在一起.抱怨的错误是什么?它似乎不喜欢联合ComponentClass<P> | StatelessComponent<P>,但那些不是元素属性,元素属性很简单<P>.我尝试明确传递<P>:
export default DragDropContext<ExampleProps>(HTML5Backend)(ExampleScreen);
Run Code Online (Sandbox Code Playgroud)
但同样的错误仍然存在.我可以通过声明输出来解决它:
export default DragDropContext(HTML5Backend)(ExampleScreen) as …Run Code Online (Sandbox Code Playgroud) 我正在研究C#泛型函数.当出错时,如果泛型类型可以是新的,则返回new T(),否则返回default(T).
像这样的代码:
private T Func<T>()
{
try
{
// try to do something...
}
catch (Exception exception)
{
if (T is new-able) // <---------- How to do this?
{
return new T();
}
else
{
return default(T);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我知道where T : new()那些使用它的人需要new T().这个问题是,如何在运行时判断这个?
在尝试从测试项目中测试Nancy模块时,遇到了一些绊脚石.我的测试代码看起来很标准:
[TestMethod]
public void Should_return_status_ok_when_route_exists()
{
// Given
var bootstrapper = new DefaultNancyBootstrapper();
var browser = new Browser(bootstrapper);
// When
var result = browser.Get("/", with =>
{
with.HttpRequest();
});
// Then
Assert.AreEqual(result.StatusCode, HttpStatusCode.OK);
}
Run Code Online (Sandbox Code Playgroud)
当模块尝试渲染视图时,我无法找到视图异常.如果我正常运行项目,模块会找到视图.只有在从测试项目调用时,模块才能找到它.
c# ×2
default ×1
generics ×1
nancy ×1
react-dnd ×1
reactjs ×1
types ×1
typescript ×1
unit-testing ×1