2月28日更新:关闭该问题,转而支持新问题.
在下面的代码片段中,如果查看最后两行,TypeScript会在第一行中显示错误,并在第二行中正确推断类型,尽管差异只是函数通过管道传递的顺序.
const pipe = <A, B, C>(
x: A,
a: (x: A) => B,
b: (x: B) => C,
) => b(a(x));
// This just calls the function passed as argument.
const call = <A, B>(f: (x: A) => B) => (x: A) => f(x)
const a = pipe(1, x => x + 1, call(x => x + 1));
const b = pipe(1, call(x => x + 1), x => x + …Run Code Online (Sandbox Code Playgroud) 如果你在 Safari 中打开这个 Fiddle https://jsfiddle.net/17uwnsq6/4/(12.1.2但应该适用于所有最新版本)并开始向下滚动白色可滚动区域,首先粘性“标题”元素将保留粘性,但稍后会滚出屏幕。在 Chrome 和 Firefox 中,它总是按预期保持粘性。
HTML 和 CSS 供参考:
<div class="title">Title</div>
<div class="container">
<div class="header">Header</div>
<div class="content">Content</div>
</div>
Run Code Online (Sandbox Code Playgroud)
<div class="title">Title</div>
<div class="container">
<div class="header">Header</div>
<div class="content">Content</div>
</div>
Run Code Online (Sandbox Code Playgroud)
当使用 flex 布局调整容器大小时,似乎会出现此错误。有人知道这个问题的解决方法吗?目标是使标题始终具有粘性,同时调整容器的大小,使其占据“标题”留下的视口部分。
TypeScript(在严格模式下)将以下函数的返回类型推断为1 | undefined:
enum E {A, B};
const f = (x: E) => {
if (x === E.A) {
return 1;
} else if (x === E.B) {
return 1;
}
}
Run Code Online (Sandbox Code Playgroud)
如何确保类型被推断为数字(因为枚举已用尽)?目前我添加了... else { throw undefined; },但这意味着如果我忘记实际用尽枚举中的所有选项,TypeScript 将无法捕捉到这一点。它与 switch 语句一起使用:
const g = (x: E) => {
switch (x) {
case E.A:
return 1;
case E.B:
return 1;
}
};
Run Code Online (Sandbox Code Playgroud)
所以这看起来很奇怪,它不适用于 if-else,而且我觉得我错过了一些明显的东西。