我隐式编写的许多方法都class
具有相同的函数类型。我想要做的是强制执行此函数类型,以便我可以明确声明某些方法必须符合该函数类型。
例如
interface MyFunctionType {(resource: string | Resource): Something}
Run Code Online (Sandbox Code Playgroud)
我的类有一些符合这个接口的方法。
class MyClass {
// ...
someMethod() { /*...*/ }
someMethodThatConforms(resource: string | Resource) {
// ...
return new Something(/*...*/);
}
anotherMethodThatConforms(resource: string | Resource) {
// ...
return new Something(/*...*/);
}
someOtherMethod() { /*...*/ }
// ...
}
Run Code Online (Sandbox Code Playgroud)
我知道这一点someMethodThatConforms
并anotherMethodThatConforms
符合接口,但现在我想知道如何断言这一点someMethodThatConforms
并且anotherMethodThatConforms
必须符合接口MyFunctionType
(这样如果我更改,MyFunctionType
就会引发错误)?
我对Javadoc很熟悉.在Javadoc中,您可以放置一个引用Javadoc的链接放在另一个类型上,如下所示:
/**
* some java thingy. see this other java thingy too {@link OtherThingy}
*/
public class Thingy { /*...*/ }
/**
* some other java thingy. see the first java thingy too {@link Thingy}
*/
public class OtherThingy{ /*...*/ }
Run Code Online (Sandbox Code Playgroud)
我可以在打字稿的JSDoc风格中做同样的事情吗?我知道我可以在评论中使用markdown,我可以放置网页链接,但这不是我想要的.
此外,任何对JSDoc/typescript文档工具的引用都会非常有用:)
编辑:根据下面的答案,这是JSDoc的一个功能,但似乎没有包含在VSCode中.VSCode中是否有有效的语法?
这是普通 ol' js 中的管道函数:
const pipe = (f, ...fs) => x =>
f === undefined ? x : pipe(...fs)(f(x))
const foo = pipe(
x => x + 1,
x => `hey look ${x * 2} a string!`,
x => x.substr(0, x.length) + Array(5).join(x.substring(x.length - 1)),
console.log
)
foo(3) // hey look 8 a string!!!!!
Run Code Online (Sandbox Code Playgroud)
我如何用类型在打字稿中写同样的东西?
即当我在管道函数时,我可以从当前最后一个函数的返回类型中获取类型信息
问题很简单:在快速路由的请求处理程序中返回值是否会执行任何操作?
我见过人们返回请求处理程序的响应:
route.get('/something', (req, res) => {
// blah blah
if (/* something */) {
return res.send('something'); // return on the same line
}
return res.send('something else');
});
Run Code Online (Sandbox Code Playgroud)
我看到人们在发送回复后提前返回:
route.get('/something', (req, res) => {
if (/* something */) {
res.send('something');
return; // return after
}
res.send('something else');
return;
});
Run Code Online (Sandbox Code Playgroud)
有什么区别吗?返回值是否被使用?
Angular 2+引入HttpClient
了发出HTTP请求并将其发送到可观察到的RxJS的功能。我的问题是,为什么我会选择在标准上使用HttpClient
的APIfetch
来发出单个HTTP请求?
我熟悉RxJS,并且了解这张“四个基本效果”图表。
| one | many
-------------------------------------
sync | T | Iterable<T>
async | Promise<T> | Observable<T>
Run Code Online (Sandbox Code Playgroud)
Http请求是异步的,并返回一个值。为什么那应该是可观察的?我知道要将事件流组合成HTTP 请求流,但是我不明白为什么我只想使用一个HTTP 响应流。当您只有一个值时,这不就是使用数组吗?
我想念什么吗?通常,为什么要使用Angular的HttpClient?哪里不符合要求?
我在提出这个问题时遇到了麻烦。
我正在寻找的是类似debounceTime
但优先的东西。
所以如果我有形状的事件{ type: 'a', priority: 2 }
。这些事件需要经过几秒钟的去抖动处理,但不是发出最后一个事件,而是发出优先级最高的事件。
input stream:
------(a|1)--(b|3)---(c|2)-----------------------(a|1)-----------------
output stream:
-----------------------------------(b|3)---------------------(a|1)-----
Run Code Online (Sandbox Code Playgroud)
我尝试查看其他运算符window
,并对最后一个事件的结果进行过滤,但这并不理想,因为window
在固定节奏上工作,我希望计时器在第一个事件上像反跳一样启动。
我正在使用 Woo GraphQL,我想知道我应该如何获取产品属性的颜色。请参阅下面的 WP Admin 中产品属性的屏幕截图:
我试过查询顶级pa-
属性,我试过在terms
/ 中查询但termNode
没有运气。
我错过了什么吗?我如何获得这些数据?
更新:TIL,产品属性中的颜色实际上是通过“WooCommerce 的变化色板”提供的。Variation Swatches 获取此信息并将其保存在元数据中。
所以现在我的问题有点不同:我如何提取术语元?
所以我有一个像这样中止控制器的提取:
async function fn() {
const abortController = new AbortController();
try {
const response = await fetch(/* ... */, { signal: abortController.signal });
// ...
} catch (e) {
// how can I tell if `e` is from a network error (e.g. offline)
// or an error from an abort
}
}
Run Code Online (Sandbox Code Playgroud)
如何判断e
是网络错误还是中止错误?
我有一个单页应用程序 + API,正在部署到 Vercel。
我目前正在尝试在 vercel.json 中添加一些配置:
/login) to
index.html`,以便我可以利用HTML History API(即react-router的浏览器路由器)/api
我有一些动态路径的端点(例如/files/[fileId]/[checksum].ts
)如何在 Vercel 中创建重写来实现此目的?
我遵循了这里的建议: https: //vercel.com/docs/configuration#routes/advanced/spa-fallback
{
"rewrites": [{ "source": "/(.*)", "destination": "/index.html" }]
}
Run Code Online (Sandbox Code Playgroud)
然而,这确实将具有动态路径的API端点重定向到index.html文件,我只想将其作为API。
有任何想法吗?
type Diff<T, U> = T extends U ? never : T; // Remove types from T that are assignable to U
type T30 = Diff<"a" | "b" | "c" | "d", "a" | "c" | "f">; // "b" | "d"
Run Code Online (Sandbox Code Playgroud)
除了删除对象的属性外,我想这样做。如何实现以下目标:
type ObjectDiff<T, U> = /* ...pls help... */;
type A = { one: string; two: number; three: Date; };
type Stuff = { three: Date; };
type AWithoutStuff = ObjectDiff<A, Stuff>; // { one: …
Run Code Online (Sandbox Code Playgroud) javascript ×4
typescript ×4
rxjs ×2
angular ×1
express ×1
fetch-api ×1
jsdoc ×1
observable ×1
promise ×1
vercel ×1
wordpress ×1
wp-graphql ×1