我想从未在特定类型接口中声明的对象中删除所有属性。
例如,假设我有以下界面:
export interface CreateCustomerUserInput {
fullname: string;
email: string;
}
Run Code Online (Sandbox Code Playgroud)
我有以下对象:
let obj = {fullname: 'AAA', email: 'aa@aa.com', phone: '111', address: 'XXX'};
Run Code Online (Sandbox Code Playgroud)
但我想创建一个仅在类型接口中声明属性的新对象。这是预期的对象:
let c = {fullname: 'AAA', email: 'aa@aa.com'}
Run Code Online (Sandbox Code Playgroud)
在 TypeScript 中有什么好的方法可以解决这个问题吗?
我们正在研究托管在同一域的两个不同子域上的两个 Web 应用程序:
https://subDomainA.domain.comhttps://subDomainB.domain.com两个Web 应用程序都显示了托管在第三个子域上的iFrame:
https://subDomainC.domain.com我们postMessage用于在顶部窗口和 IFrame 窗口之间进行通信。
在subDomainA 上显示的 iFrame 中,我们设置了一个 AuthToken:
localStorage.setItem("AuthToken", "JWTAuthToken")
当我们导航到subDomainB并尝试在 iFrame 中运行以下代码时:
localStorage.getItem("AuthToken")
结果是null。
domain.com)这是 Safari 的固有行为还是我们的实现有问题?
给定一个这样的计数器在给定时间范围内增加,计算和http_requests_total有何区别?delta(http_requests_total[5m])increase(http_requests_total[5m])
据我理解的文档,delta计算时间范围的开始值和结束值之间的差。increase计算rate,然后将其与时间范围相乘。
但实际的区别是什么?这两个值不是总是相同吗?
就像,假设我有以下值,每个值都在下一秒:
t0: 5
t1: 11
t2: 18
t3: 30
Run Code Online (Sandbox Code Playgroud)
那么delta就会是30 - 5 = 25。这rate将是各个增量的平均值,即8.333。如果我将其乘以时间范围3,那么我会25再次得到。那么两者实际上有什么区别呢?
假设我有一个接口
interface I <T extends InternalResult> {
doStuff(): T;
}
Run Code Online (Sandbox Code Playgroud)
和两个实现这个接口的具体类:
class A implements I <InternalAResult> {
doStuff(): InternalAResult {
// implementation
}
}
class B implements I <InternalBResult> {
doStuff(): InternalBResult {
// implementation
}
}
Run Code Online (Sandbox Code Playgroud)
我的客户端不能doStuff()直接执行,但需要一个实现Executer接口的类的实例,例如:
interface Executer <T, R> {
execute(it: T): R;
}
class AExecuter implements Executer <A, AResult> {
execute(it: A): AResult {
let internalResult = it.doStuff();
// ... do something specific with internalResult create result
return result;
}
}
class …Run Code Online (Sandbox Code Playgroud) 假设我有一个可观察的项目
let items$ = Observable<Item[]>
Run Code Online (Sandbox Code Playgroud)
每个 Item 都有一个 property isSelected$,它本身就是一个 Observable:
private isSelected$: Observable<boolean>
Run Code Online (Sandbox Code Playgroud)
接收当前选定的所有项目的列表的最佳方式是什么?我的解决方案看起来不太正确:
items$.pipe(
switchMap(items =>
combineLatest(items.map(item =>
item.isSelected$.pipe(
map(isSelected => ({item: item, isSelected: isSelected})))),
map(items => items.filter(item => item.isSelected).map(item => item.item))
)))
Run Code Online (Sandbox Code Playgroud)
它很有效,我以前也用过它,但对于我必须经常做的事情来说,它的结构非常复杂。一定会有更好的办法。
注意:订阅者需要所有选定项目的列表,而不是单个项目的流。
示例:
items$将发出以下列表:
[{id: 1, isSelected$: of(true)},
{id: 2, isSelected$: of(false)},
{id:3, isSelected$: of(true)}]
Run Code Online (Sandbox Code Playgroud)
作为我们订阅的结果,我们希望获得列表:
[{id: 1}, {id: 3}]
Run Code Online (Sandbox Code Playgroud) 我通过2 [formControl]"toppings"收听2个表单:array和"toppings2":array.
我必须同时拥有表格的2个值.所以我把我的两个观察者与"CombineLatest"结合起来
我的代码:
ngOnInit() {
combineLatest(this.toppings.valueChanges, this.toppings2.valueChanges)
.subscribe(val => console.log(val))
}
Run Code Online (Sandbox Code Playgroud)
但是现在,在组件的初始化中,只有一个表单发送console.log(val).
如果我在收到第二张表格的日志后点击此表格.你遇到这个问题吗?
我的 TypeScript 代码中有以下常见场景:
interface TopLevelInterface<A extends BottomLevelInterface<B>, B>
Run Code Online (Sandbox Code Playgroud)
其实现BottomLevelInterface如下所示:
class BottomLevelClass implements BottomLevelInterface<MyType>
Run Code Online (Sandbox Code Playgroud)
我的问题是:在实现时,TopLevelInterface我不仅需要传递 的类型参数A,BottomLevelClass还需要传递B上面MyType示例中的第二个类型参数。
为什么我需要指定B通过查看 的类型参数可以轻松推断出哪些内容BottomLevelClass?
例如,在实现时TopLevelInterface我需要指定以下内容:
class TopLevelClass implements TopLevelInterface<ConcreteBottomLevel, MyType>
Run Code Online (Sandbox Code Playgroud)
而不是更短的版本就足够了:
class TopLevelClass implements TopLevelInterface<ConcreteBottomLevel>
Run Code Online (Sandbox Code Playgroud)
为什么这是必要的?如何通过查看第一个类型参数来推断第二个类型参数?我想到的唯一解决方案是infer在 TypeScript 2.8+ 中使用默认分配。这个解决方案看起来像这样:
interface TopLevelInterface<A extends BottomLevelInterface<B>,
B = A extends BottomLevelInterface<infer _B> ? _B : any>
Run Code Online (Sandbox Code Playgroud)
但是,我无法将其正确应用于三层类层次结构。model在下面的 StackBlitz 中,您可以看到我无法获得的属性的正确类型约束TopLevelClass。它应该被推断为类型SomeType,但却被推断为never。它MiddleLevelClass确实工作正常。
https://stackblitz.com/edit/typescript-pcxnzo?file=infer-generics.ts …
typescript ×4
javascript ×2
rxjs ×2
angular ×1
generics ×1
iframe ×1
prometheus ×1
promql ×1
safari ×1
subscribe ×1