在 CSS 中,position: sticky使元素能够以某种position: static行为进行显示(即,它采用其在文档流中的默认位置),直到它到达某个滚动位置,然后采用某种position: fixed行为。
position: sticky那么...这是否意味着我们不能在需要正常行为的元素上使用position: absolute?
语境:
我有一个流出元素,它占据了视口左上角的位置。滚动一两英寸后,元素会到达视口的顶部,理想情况下,我希望它不会在此时继续消失。
如何根据对象数组的条件获取所有索引?我已经尝试了下面的代码,但它只返回第一次出现的情况。
a = [
{prop1:"abc",prop2:"yutu"},
{prop1:"bnmb",prop2:"yutu"},
{prop1:"zxvz",prop2:"qwrq"}];
index = a.findIndex(x => x.prop2 ==="yutu");
console.log(index);Run Code Online (Sandbox Code Playgroud)
如果联合类型的成员共享一个属性,并且该属性的类型可用于区分这些成员,那么我应该能够使用作为条件来缩小if子句中的类型范围typeof。但这不起作用。
例如,在if下面的子句中, 的类型event应推断为UserTextEvent, 的类型event.target应推断为HTMLInputElement:
type UserTextEvent = { value: string, target: HTMLInputElement };
type UserMouseEvent = { value: [number, number], target: HTMLElement };
type UserEvent = UserTextEvent | UserMouseEvent
function handle(event: UserEvent) {
if (typeof event.value === 'string') {
event.value // string, as expected
event.target // should be narrowed to HTMLInputElement, but
// is still HTMLInputElement | HTMLElement. Why?
}
}
Run Code Online (Sandbox Code Playgroud) type-inference discriminated-union narrowing typescript union-types
我正在使用 TypeScript 和 Rollup 的组合,如本文中所示。
因此math.ts
export function square(x: number) {
return x ** 2;
}
export function cube(x: number) {
return x ** 3;
}
Run Code Online (Sandbox Code Playgroud)
和main.ts
import { square } from "./math";
console.log(square(3));
Run Code Online (Sandbox Code Playgroud)
命令后生成
tsc -t ES5 -m es2015 && rollup -f es -o app.js -m -i main.js
Run Code Online (Sandbox Code Playgroud)
文件app.js
function square(x) {
return Math.pow(x, 2);
}
console.log(square(3));
//# sourceMappingURL=app.js.map
Run Code Online (Sandbox Code Playgroud)
但生成的源映射指向 的.js输出tsc,而不是原始.ts文件。我怎样才能得到后者?
我知道正式的描述:
设置:创建测试的预期状态。
拆卸:进行必要的清理操作。
但是,为什么这是必要的,尤其是在 Benchmark.js 中?为什么需要不同的测试周期(如 Benchmark.js 中所定义)中 Benchmark.js 中所定义)?我观察到,在我能想到的所有情况下(我也认为在所有其他情况下),您可以将设置代码移至准备代码(基准/测试之外的代码),也许将代码拆解到代码末尾,功能本质上是相同的(我也查看了一些 jsperf.com 测试,据我所知,它们也是如此)。
例如,这是我创建的基准测试,该版本使用设置和拆卸:
const bench = new Benchmark(
'TicTacToeBenchmark',
// The function to test
() => {
ticTacToe.addEvent(
'turn',
player => {
turnText.innerHTML =
'It\'s ' + (player['id'] === 1 ? 'X' : 'O') + '\'s turn.';
}
);
},
{
'setup': () => {
const players = [
{
char: '✕',
className: 'playerX',
id: 1,
},
{
char: '◯',
className: 'playerY',
id: 2,
},
]; …Run Code Online (Sandbox Code Playgroud) 我正在计划一个 monorepo typescript 项目,如下所示:
/ (root)
+--backend/
| +-src/
| \-tsconfig.json
+--shared/
| \-src/
\--frontend/
\-src/
Run Code Online (Sandbox Code Playgroud)
tsconfig.json定义如下:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"outDir": "./dist",
"strict": true,
"baseUrl": "./src",
"paths": {
"shared": [
"../../shared/src"
]
},
"rootDirs": [
"./src",
"../shared/src"
],
"esModuleInterop": true
}
}
Run Code Online (Sandbox Code Playgroud)
tsc当我在它下面执行时backend,如下所示:
/ (root)
+-backend/
+-dist/
| +-backend/
| | +-src/
| \-shared/
| \-src/
+-src/
\-tsconfig.json
Run Code Online (Sandbox Code Playgroud)
在上面,dist包含backend和shared但它们每个都包含src在它下面。我想要backend并shared包含dist …
我的包裹是这样的:
? tsconfig.json
? src/
? ? index.ts (import './dependence.ts')
? ? dependence.ts
? example/
? index.html
? script.ts (import '../src/index.ts')
Run Code Online (Sandbox Code Playgroud)
我想
./src/*.ts 编译在 ./dist/*.js./example/*.ts 编译在 ./example/*.js运行后tsc,我希望我的包看起来像这样:
? tsconfig.json
? src/
? ? index.ts (import './dependence.ts')
? ? dependence.ts
?!dist/
? ?!index.js (import './dependence.js')
? ?!dependence.js
? example/
? index.html
? script.ts (import '../src/index.ts')
?!script.js (import '../dist/index.js')
Run Code Online (Sandbox Code Playgroud)
我对所有 tsconfig 选项都有些困惑。
我已经尝试了很多选项,如baseUrl, paths, rootDir, outDir, rootDirs, ... 都没有成功。
如何缩小别名条件表达式中类的属性类型?
简而言之:在类方法中,我想要执行类型缩小检查,例如this._end === null && this._head === null,但我想首先将此检查的结果分配给变量,然后再在类型缩小if子句中使用它。它没有按预期工作。
不短:假设我们有一个Queue类:
class Queue {
private _length: number = 0;
private _head: Node | null = null;
private _end: Node | null = null;
public enqueue(node: Node): boolean {
this._length += 1;
const isQueueEmpty = this._end === null;
if (isQueueEmpty) {
this._head = node;
this._end = node;
return true;
}
this._end.setLink(node); // TypeScript doesn't understand that this._end passed the null check
// in the if …Run Code Online (Sandbox Code Playgroud) javascript typescript type-narrowing typeguards typescript4.0
给定此类型别名:
export type RequestObject = {
user_id: number,
address: string,
user_type: number,
points: number,
};
Run Code Online (Sandbox Code Playgroud)
我想要一个包含其所有属性的数组,例如:
['user_id','address','user_type','points']
Run Code Online (Sandbox Code Playgroud)
有什么选择可以得到这个吗?我已经用谷歌搜索过,但我只能使用以下包获取它的界面
当尝试拉取(或签出或挑选)涉及文件名仅在大小写方面发生变化的文件的提交时,我从 git 收到以下错误:
The following untracked working tree files would be overwritten by merge
Run Code Online (Sandbox Code Playgroud)
之后它会列出所有仅大小写差异的文件。Agit status在我的工作目录中没有显示任何内容。
在首先删除相关文件的本地副本后,我能够pull成功执行该操作,但我想知道是否有一种更优雅的方法来解决,或者更好的是,首先避免该问题。在本地计算机上具有相同存储库的 MacOS 上运行的其他用户没有此类症状/问题。
typescript ×6
javascript ×3
tsconfig ×2
arrays ×1
benchmark.js ×1
benchmarking ×1
css ×1
css-position ×1
dependencies ×1
git ×1
macos ×1
monorepo ×1
narrowing ×1
rollupjs ×1
source-maps ×1
sticky ×1
tsc ×1
typeguards ×1
union-types ×1
unit-testing ×1
vue.js ×1