Eshu

 view release on metacpan or  search on metacpan

t/0247-realworld-ts.t  view on Meta::CPAN


class InMemoryRepo<T extends { id: number }> implements Repository<T> {
    private store = new Map<number, T>();

    async findById(id: number): Promise<T | null> {
        return this.store.get(id) ?? null;
    }

    async findAll(): Promise<T[]> {
        return Array.from(this.store.values());
    }

    async save(entity: T): Promise<T> {
        this.store.set(entity.id, entity);
        return entity;
    }

    async delete(id: number): Promise<void> {
        this.store.delete(id);
    }
}
END
    is(ts($code), $code, 'TS: generic in-memory repository');
}

# 13. readonly tuple
{
    my $code = <<'END';
type RGB = readonly [number, number, number];
type Point2D = readonly [number, number];

function addVectors(a: Point2D, b: Point2D): Point2D {
    return [a[0] + b[0], a[1] + b[1]];
}

function blendColors(a: RGB, b: RGB, t: number): RGB {
    return [
        Math.round(a[0] + (b[0] - a[0]) * t),
        Math.round(a[1] + (b[1] - a[1]) * t),
        Math.round(a[2] + (b[2] - a[2]) * t),
    ];
}
END
    is(ts($code), $code, 'TS: readonly tuples');
}

# 14. namespace
{
    my $code = <<'END';
namespace Validation {
    export interface Validator<T> {
        validate(value: unknown): value is T;
        message: string;
    }

    export function isString(v: unknown): v is string {
        return typeof v === 'string';
    }

    export function isNumber(v: unknown): v is number {
        return typeof v === 'number' && !isNaN(v);
    }
}
END
    is(ts($code), $code, 'TS: namespace');
}

# 15. overloaded function
{
    my $code = <<'END';
function format(value: string): string;
function format(value: number, decimals?: number): string;
function format(value: Date, locale?: string): string;
function format(value: string | number | Date, extra?: number | string): string {
    if (typeof value === 'string') {
        return value.trim();
    }
    if (typeof value === 'number') {
        return value.toFixed(extra as number ?? 2);
    }
    return (value as Date).toLocaleDateString(extra as string);
}
END
    is(ts($code), $code, 'TS: function overloads');
}

# 16. template literal type
{
    my $code = <<'END';
type EventName = 'click' | 'focus' | 'blur';
type HandlerName = `on${Capitalize<EventName>}`;

type CSSUnit = 'px' | 'rem' | 'em' | '%';
type CSSValue = `${number}${CSSUnit}`;

type EventMap = {
    [K in EventName as `on${Capitalize<K>}`]: (event: Event) => void;
};
END
    is(ts($code), $code, 'TS: template literal types');
}

# 17. abstract class
{
    my $code = <<'END';
abstract class Shape {
    abstract area(): number;
    abstract perimeter(): number;

    describe(): string {
        return `area=${this.area().toFixed(2)}, perimeter=${this.perimeter().toFixed(2)}`;
    }
}

class Rectangle extends Shape {
    constructor(private width: number, private height: number) {
        super();
    }

    area(): number {
        return this.width * this.height;



( run in 0.879 second using v1.01-cache-2.11-cpan-60884a8df76 )