In the current version (2.1) of TypeScript I can constrain a method argument on a generic class to be a property of the generic type.
class Foo<TEntity extends {[key:string]:any}> {
public bar<K extends keyof TEntity>(key:K, value:TEntity[K]) { }
}
Run Code Online (Sandbox Code Playgroud)
Is it possible in the current type system to constrain the key part even further to be a subset where the value of the key is of a certain type?
What I'm looking for is something along the lines of this …
我有一个带有内容区域的布局,它有自己的滚动条,因为我希望侧边栏独立滚动。
使用底部的按钮动态附加内容区域的内容。
当内容区域的滚动位置位于最顶部(scrollTop === 0)并且我附加更多内容时,滚动位置保持在顶部。然而,如果滚动位置不在顶部,Firefox 和 Chrome 的行为会有所不同。在 Firefox 中,滚动位置保持相对于顶部,内容附加在下方,如有必要,可在屏幕外附加。然而,在 Chrome 中,滚动位置相对于底部保持不变,将按钮保持在相同位置,导致用户必须向上滚动才能看到新内容。
有没有办法让 Chrome 表现得像 Firefox 一样?
澄清。添加新内容后,我不希望内容区域滚动到底部。我希望它保留在原来的位置,以便用户不必向上滚动即可看到新内容。
var count = 1;
window.addEventListener('load', function() {
var container = document.querySelector('[data-result]');
var button = document.querySelector('button');
button.addEventListener('click', function() {
for (var i = 0; i < 10; i++) {
var element = document.createElement('div');
element.innerHTML = 'Content row ' + count++;
container.appendChild(element);
}
});
});Run Code Online (Sandbox Code Playgroud)
html,
body,
.page {
height: 100vh;
margin: 0;
}
body {
background-color: #eceff1;
}
.page {
display: flex;
flex-direction: …Run Code Online (Sandbox Code Playgroud)