mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
WIP: implementation of input function signature
This commit is contained in:
@@ -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>;
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user