在 JavaScript 中:
function doSomething(mybar) {
if (!(mybar instanceof Bar)) throw new TypeError("mybar");
// ...other guard clauses
}
Run Code Online (Sandbox Code Playgroud)
在打字稿中:
function doSomething(mybar: Bar) {
// is guard clause ALWAYS redundant?
// ...other guard clauses
}
Run Code Online (Sandbox Code Playgroud)
我喜欢用保护子句验证输入。因此,在 JavaScript 中,我会测试它是否mybar是 的一个实例Bar,如果不是,那么我会抛出一个TypeError.
在 TypeScript 中mybar保证是正确的类型?(从而消除了对第一个保护条款的需要)
更新
下面有一些很好的答案,范围从“这绝对有可能”到“它可能发生”到“它不可能发生”。
因此,也许表达问题的一个好方法是 - 如果可能在运行时提供错误的类型,那么发生这种情况的机制是什么?例如,打字中的错误?
这不会编译:
public hello(user?: User): void {
// ...do some stuff
console.log(user.toString()); // error: "Object is possibly undefined"
}
Run Code Online (Sandbox Code Playgroud)
这可以通过类型保护来修复:
if (!user) throw Error();
Run Code Online (Sandbox Code Playgroud)
但我想把它移到一个单独的函数中,所以我试过了
private guard(arg: unknown): arg is object {
if (!arg) throw Error();
}
Run Code Online (Sandbox Code Playgroud)
我试过
private guard(arg: unknown): arg is object {
return arg !== undefined;
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用。
如何在单独的函数中编写自定义类型保护,以断言“未定义”?
我正在使用ASP.NET Core 2.1。我认为静态文件中间件应该先于mvc中间件-无需通过mvc运行请求即可css(例如)来提供文件。
所以我按以下顺序排列它们:
app.UseExceptionHandler(/*...*/)
app.UseHsts();
app.UseHttpsRedirection();
app.UseStatusCodePagesWithReExecute(/*...*/);
// and lastly:
app.UseStaticFiles();
app.UseMvc(/*...*/);
Run Code Online (Sandbox Code Playgroud)
但是,当我打开调试级别日志记录时,我注意到如果缺少静态文件,它将运行Microsoft.AspNetCore.Builder.RouterMiddleware并显示Request did not match any routes,然后运行my ErrorController并为该请求发出404。
所以:
ASP.NET 核心 2.1。
我的图标位于/images/favicon.ico且无法更改。所以页面包括:
<link href="/images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
Run Code Online (Sandbox Code Playgroud)
那行得通。
但直接请求example.com/favicon.ico将会失败并返回 404。
我如何重定向example.com/favicon.ico到example.com/images/favicon.ico?
是否有一个“重复最后一个命令”的命令?如果没有,我该如何设置?
基本上,我想要的是按下一些快捷方式,并使其重复上一个命令,所以我不必在菜单或ctrl-shift-p框中再次找到它。
环境:ASP.NET Core 2.1,Ubuntu。
在旧式 ASP.NET 中,当我进行 bin 部署(例如上传一些 dll 文件)时,webapp 会检测到并重新加载自己 - 非常有用。
使用 Core,它不会那样做。我需要停止并重新启动该dotnet MyApp.dll过程。
如何让它检测对二进制文件的更改并重新加载?
asp.net ubuntu kestrel-http-server asp.net-core asp.net-core-2.1
我在很多地方都使用了一个模拟,所以我想把它移到一个可以重复使用的单独文件中。
我认为 Jest 称其为“手动模拟”。但是我不想使用__mocks__约定。
被测试文件的顶部:
import * as dotenvSafe from "dotenv-safe";
Run Code Online (Sandbox Code Playgroud)
手动模拟文件:
const dotenvSafe: any = jest.genMockFromModule("dotenv-safe");
dotenvSafe.load = jest.fn(() => { // the function I want to mock
return {
error: undefined,
parsed: [],
};
});
export default dotenvSafe;
Run Code Online (Sandbox Code Playgroud)
在测试文件的顶部,我尝试了各种方法:
jest.setMock("dotenv-safe", "../../mocks/dotenv-safe");
不起作用。被测试的代码获取"../../mocks/dotenv-safe.mock"而不是模块。
jest.mock("dotenv-safe", () => require("../../mocks/dotenv-safe"));
不起作用 - 被测试的代码抛出TypeError: dotenvSafe.load is not a function.
jest.mock("dotenv-safe", () => { return { load: jest.fn(() => ({error: undefined, parsed: []})) }; });
行得通!但是模拟是内联的,我想将它移动到一个单独的文件中。我不想在每个文件中重复这一点。
什么是正确的语法?
在我的测试代码中,我想模拟router:
import * as express from "express";
const router = express.Router(); // I want to mock this
router.route(...).post(...);
router.route(...).get(...);
Run Code Online (Sandbox Code Playgroud)
在我的测试中:
import * as express from "express";
test("foo", () => {
jest.mock("express", () => {
Router: () => jest.fn()
});
// ...test stuff
});
Run Code Online (Sandbox Code Playgroud)
但这是行不通的。我究竟做错了什么?
It is possible to bind actions' parameters via:
[FromBody] Request body[FromForm] Form data in the request body[FromHeader] Request header[FromQuery] Request query string parameter[FromRoute] Route data from the current request[FromServices]I often need to extract something from a JWT, almost always the id (primary key). So I do this (ignore error checking for now):
var id = int.Parse(base.User.FindFirst(ClaimTypes.NameIdentifier)?.Value);
Run Code Online (Sandbox Code Playgroud)
It would be great if I could put that into an attribute binder that would work like this: …
假设我有一些返回 observable 的操作。
this.loginService
.post<IResponse>(url, { username, password })
.subscribe(
next => { /* I know the type of next */ },
error => { /* Can I specify type of err, or is it always any? */ }
);
Run Code Online (Sandbox Code Playgroud)
类型next是我可以设置的 - 在这种情况下IResponse。
怎么样error- 我可以以某种方式指定它的类型吗?还是总是这样any,我总是需要在处理程序中进行类型转换?
typescript ×5
asp.net-core ×4
c# ×2
jestjs ×2
node.js ×2
angular ×1
asp.net ×1
express ×1
favicon ×1
jwt ×1
mocking ×1
rxjs ×1
ubuntu ×1
unit-testing ×1