 ```ts
export class LruCache<K, V> {
    private capacity: number;
    private map: Map<K, V>;
    private order: K[];

    constructor(capacity: number) {
        if (typeof capacity !== 'number' || capacity <= 0) {
            throw new Error('Capacity must be a positive integer');
        }
        this.capacity = capacity;
        this.map = new Map();
        this.order = [];
    }

    get(key: K): V | undefined {
        if (!this.map.has(key)) {
            return undefined;
        }
        const value = this.map.get(key);
        // Move the key to the end of the order array to indicate it was recently used
        this.order.splice(this.order.indexOf(key), 1);
        this.order.push(key);
        return value;
    }

    set(key: K, value: V): void {
        if (this.map.has(key)) {
            // Update the value and mark it as recently used
            this.map.set(key, value);
            this.order.splice(this.order.indexOf(key), 1);
            this.order.push(key);
        } else {
            if (this.map.size >= this.capacity) {
                // Evict the least recently used item
                const lruKey = this.order.shift();
                if (lruKey !== undefined) {
                    this.map.delete(lruKey);
                }
            }
            // Add the new key-value pair and mark it as recently used
            this.map.set(key, value);
            this.order.push(key);
        }
    }

    get size(): number {
        return this.map.size;
    }
}
```