tr1::shared_ptr和之间有什么区别boost::shared_ptr吗?如果是这样,什么?
我正在使用 typescript-eslint v3.1.0 并在 TypeScript 中具有以下类型保护功能:
interface JWTPayload {
sub: number;
}
function isJWTPayload(obj: unknown): obj is JWTPayload {
if (typeof obj !== 'object') {
return false;
}
// eslint-disable-next-line @typescript-eslint/ban-types
const obj2 = obj as object;
if (!('sub' in obj2)) {
return false;
}
const obj3 = obj2 as JWTPayload;
if (!Number.isInteger(obj3.sub)) {
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是这样的:在我想象的常见场景中禁用 lint 规则感觉不太好。是否有 TypeScript 模式可以避免这种情况?
一些背景:
被禁用的 typescript-eslint 规则是在https://github.com/typescript-eslint/typescript-eslint/pull/848中引入的,其中认为“99.9%的时间,你不希望使用[“对象”类型],而绝大多数代码库都不想使用它”。他们为什么会这么说?似乎每当您验证用户输入时都会使用它。有没有其他方法可以在不强制转换为“any”的情况下做到这一点?
假设您具有以下一对功能:
void f(const int&) {
// Do something, making a copy of the argument.
}
void f(int&&) {
// Do the same thing, but moving the argument.
}
Run Code Online (Sandbox Code Playgroud)
它们是相当多余的-函数之间的唯一区别是它们是复制还是移动参数。当然,通过将其重写为单个模板函数,我们可以做得更好:
template<typename T>
void g(T&&) {
// Do something, possibly using std::forward to copy or move the argument.
}
Run Code Online (Sandbox Code Playgroud)
这有效,并且在实践中是常用的惯用语。但是该模板可能被实例化为三个函数,而上面的两个函数又是如此。我们可以使用以下代码来验证这种情况:
#include <iostream>
template<typename T> constexpr char *type = nullptr;
template<> constexpr const char *type<int&> = "int&";
template<> constexpr const char *type<const int&> = "const int&";
template<> constexpr const char *type<int> = "int"; …Run Code Online (Sandbox Code Playgroud) c++ templates perfect-forwarding c++11 template-argument-deduction
c++ ×2
boost ×1
c++11 ×1
eslint ×1
shared-ptr ×1
template-argument-deduction ×1
templates ×1
tr1 ×1
typescript ×1