WIP: implementation of input function signature

This commit is contained in:
Paul Gschwendtner
2023-06-01 13:36:34 +00:00
parent f050c5b19a
commit d49aec75df
2 changed files with 46 additions and 8 deletions
+22 -3
View File
@@ -6,7 +6,26 @@ export interface InputOptions {
// TODO: transform
}
// WIP starting point
export function input<T>(defaultVal: T, opts?: InputOptions): Signal<T> {
return signal(defaultVal);
export interface InputOptionsWithInitialValue<T> extends InputOptions {
initialValue?: T;
}
// it transforms our specific types into primitive origins
type toPrimitive<T> =
T extends string ? string : T extends number ? number : T extends boolean ? boolean : T;
type primitiveValue = string|number|boolean|BigInt|undefined|null|Symbol
export function input<T>(): Signal<T|undefined>;
export function input<T>(opts: InputOptionsWithInitialValue<T>): Signal<T>;
export function input<T extends primitiveValue>(
defaultVal: T, opts?: InputOptions): Signal<toPrimitive<T>>;
export function input<T extends primitiveValue|InputOptionsWithInitialValue<Y>, Y>(
defaultValOrOpts?: T, opts?: InputOptions): Signal<T> {
// TODO(signals): implement
return signal(
typeof defaultValOrOpts === 'object' ?
(defaultValOrOpts as InputOptionsWithInitialValue<T>).initialValue :
defaultValOrOpts) as unknown as Signal<T>;
}
+24 -5
View File
@@ -1,19 +1,38 @@
import {Component, input} from '@angular/core';
import {Component, input, Input} from '@angular/core';
import {bootstrapApplication} from '@angular/platform-browser';
@Component({
selector: 'greet',
standalone: true,
signals: true,
template: `{{ nameInput() }}`,
template: `{{ counter() }}`,
})
class Greet {
nameInput = input('Jeff');
export class Greet<T> {
counter = input(0);
bla = input(); // TODO: should be a diagnostic. no type & no value
bla2 = input<string>()
bla3 = input({initialValue: 3, required: true});
bla4 = input(3, {required: true, alias: 'bla4Public'})
gen = input<T>({required: true});
gen2 = input<T>();
works(): T {
return this.gen();
}
// TODO: should break, but still supported early prototype.
@Input() oldInput: string|undefined;
}
@Component({
standalone: true,
template: `Hello <greet [nameInput]="'Bla'" />`,
template: `
Hello <greet [counter]="3" [bla4Public]="10" #ok
[bla3]="-10" [gen]="{yes: true}"
/>
<button (click)="ok.works().yes">Click</button>
`,
imports: [Greet],
})
export class MyApp {