admin管理员组文章数量:1026989
If I have something like this:
import { Component, output } from '@angular/core';
@Component({...})
export class MyComponent {
success= output<{message: string, response: SomeModelNotExported}>;
}
I am currently doing a Type extraction via indexed access and conditional inference
onSuccess(event: MyComponent['success'] extends OutputEmitterRef<infer T> ? T : never) { // type is inferred here as {message: string, response: SomeModelNotExported}
...
}
It just looks too bloody. Is there a more friendlier approach to this? Am I missing some already available types exported by Angular?
If I have something like this:
import { Component, output } from '@angular/core';
@Component({...})
export class MyComponent {
success= output<{message: string, response: SomeModelNotExported}>;
}
I am currently doing a Type extraction via indexed access and conditional inference
onSuccess(event: MyComponent['success'] extends OutputEmitterRef<infer T> ? T : never) { // type is inferred here as {message: string, response: SomeModelNotExported}
...
}
It just looks too bloody. Is there a more friendlier approach to this? Am I missing some already available types exported by Angular?
Share Improve this question edited Feb 28 at 3:40 Alex Pappas asked Feb 20 at 5:49 Alex PappasAlex Pappas 2,6783 gold badges31 silver badges61 bronze badges 1 |1 Answer
Reset to default 1You can explicitely define a type, which accepts the generic as an input, then use this generic type across your component.
export type OutputType<T> = {
message: string;
response: T;
};
@Component({ selector: 'my-component', template: '' })
export class MyComponent<TResponse> {
success = output<OutputType<TResponse>>();
onSuccess(event: OutputType<TResponse>) {
console.log(event.message);
}
}
If I have something like this:
import { Component, output } from '@angular/core';
@Component({...})
export class MyComponent {
success= output<{message: string, response: SomeModelNotExported}>;
}
I am currently doing a Type extraction via indexed access and conditional inference
onSuccess(event: MyComponent['success'] extends OutputEmitterRef<infer T> ? T : never) { // type is inferred here as {message: string, response: SomeModelNotExported}
...
}
It just looks too bloody. Is there a more friendlier approach to this? Am I missing some already available types exported by Angular?
If I have something like this:
import { Component, output } from '@angular/core';
@Component({...})
export class MyComponent {
success= output<{message: string, response: SomeModelNotExported}>;
}
I am currently doing a Type extraction via indexed access and conditional inference
onSuccess(event: MyComponent['success'] extends OutputEmitterRef<infer T> ? T : never) { // type is inferred here as {message: string, response: SomeModelNotExported}
...
}
It just looks too bloody. Is there a more friendlier approach to this? Am I missing some already available types exported by Angular?
Share Improve this question edited Feb 28 at 3:40 Alex Pappas asked Feb 20 at 5:49 Alex PappasAlex Pappas 2,6783 gold badges31 silver badges61 bronze badges 1-
I'm curious how angular infers the raw value type from the template when you are using the
$event
in the output binding as well. – Alex Pappas Commented Feb 20 at 5:51
1 Answer
Reset to default 1You can explicitely define a type, which accepts the generic as an input, then use this generic type across your component.
export type OutputType<T> = {
message: string;
response: T;
};
@Component({ selector: 'my-component', template: '' })
export class MyComponent<TResponse> {
success = output<OutputType<TResponse>>();
onSuccess(event: OutputType<TResponse>) {
console.log(event.message);
}
}
本文标签:
版权声明:本文标题:typescript - How to extractinfer the raw value type from a Signal, Input, or Output of Angular? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1741456965a1869949.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$event
in the output binding as well. – Alex Pappas Commented Feb 20 at 5:51