Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Angular: Add type support for Angular's output signals #26546

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion code/frameworks/angular/scripts/postbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ const path = require('path');

const filePath = path.join(__dirname, '../dist/client/public-types.d.ts');
const fileContent = fs.readFileSync(filePath, 'utf8');
const newContent = fileContent.replaceAll(/(type AngularInputSignal)/g, '// @ts-ignore\n$1');
const newContent = fileContent
.replaceAll(/(type AngularInputSignal)/g, '// @ts-ignore\n$1')
.replaceAll(/(type AngularOutputEmitterRef)/g, '// @ts-ignore\n$1');
fs.writeFileSync(filePath, newContent, 'utf8');
17 changes: 13 additions & 4 deletions code/frameworks/angular/src/client/public-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,30 @@ export type Preview = ProjectAnnotations<AngularRenderer>;
/**
* Utility type that transforms InputSignal and EventEmitter types
*/
type TransformComponentType<T> = TransformInputSignalType<TransformEventType<T>>
type TransformComponentType<T> = TransformInputSignalType<TransformOutputSignalType<TransformEventType<T>>>

// @ts-ignore Angular < 17.2 doesn't export InputSignal
type AngularInputSignal<T> = AngularCore.InputSignal<T>
// @ts-ignore Angular < 17.2 doesn't export InputSignalWithTransform
type AngularInputSignalWithTransform<T, U> = AngularCore.InputSignalWithTransform<T, U>
// @ts-ignore Angular < 17.3 doesn't export AngularOutputEmitterRef
type AngularOutputEmitterRef<T> = AngularCore.OutputEmitterRef<T>

type AngularHasSignal = typeof AngularCore extends { input: infer U } ? true : false;
type InputSignal<T> = AngularHasSignal extends true ? AngularInputSignal<T> : never;
type InputSignalWithTransform<T, U> = AngularHasSignal extends true ? AngularInputSignalWithTransform<T, U> : never;
type AngularHasInputSignal = typeof AngularCore extends { input: infer U } ? true : false;
type AngularHasOutputSignal = typeof AngularCore extends { output: infer U } ? true : false;

type InputSignal<T> = AngularHasInputSignal extends true ? AngularInputSignal<T> : never;
type InputSignalWithTransform<T, U> = AngularHasInputSignal extends true ? AngularInputSignalWithTransform<T, U> : never;
type OutputEmitterRef<T> = AngularHasOutputSignal extends true ? AngularOutputEmitterRef<T> : never;

type TransformInputSignalType<T> = {
[K in keyof T]: T[K] extends InputSignal<infer E> ? E : T[K] extends InputSignalWithTransform<any, infer U> ? U : T[K];
};

type TransformOutputSignalType<T> = {
[K in keyof T]: T[K] extends OutputEmitterRef<infer E> ? (e: E) => void : T[K];
valentinpalkovic marked this conversation as resolved.
Show resolved Hide resolved
};

type TransformEventType<T> = {
[K in keyof T]: T[K] extends AngularCore.EventEmitter<infer E> ? (e: E) => void : T[K];
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, Input, Output, EventEmitter, input } from '@angular/core';
import { Component, Input, input, output } from '@angular/core';

@Component({
// Needs to be a different name to the CLI template button
Expand Down Expand Up @@ -40,8 +40,7 @@ export default class SignalButtonComponent {
/**
* Optional click handler
*/
@Output()
onClick = new EventEmitter<Event>();
onClick = output<Event>();
valentinpalkovic marked this conversation as resolved.
Show resolved Hide resolved

valentinpalkovic marked this conversation as resolved.
Show resolved Hide resolved
public get classes(): string[] {
const mode = this.primary() ? 'storybook-button--primary' : 'storybook-button--secondary';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, Input, Output, EventEmitter, input } from '@angular/core';
import { Component, Input, input, output } from '@angular/core';

@Component({
// Needs to be a different name to the CLI template button
Expand Down Expand Up @@ -40,8 +40,7 @@ export default class SignalButtonComponent {
/**
* Optional click handler
*/
@Output()
onClick = new EventEmitter<Event>();
onClick = output<Event>();

public get classes(): string[] {
const mode = this.primary() ? 'storybook-button--primary' : 'storybook-button--secondary';
Expand Down
Loading