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

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

  get(key: K): V | undefined {
    if (this.cache.has(key)) {
      const index = this.order.findIndex(k => k === key);
      this.order.splice(index, 1);
      this.order.push(key);
      return this.cache.get(key);
    }
    return undefined;
  }

  set(key: K, value: V): void {
    if (this.cache.has(key)) {
      this.order = this.order.filter(k => k !== key);
    } else if (this.cache.size >= this.capacity) {
      const oldestKey = this.order.shift();
      if (oldestKey !== undefined) {
        this.cache.delete(oldestKey);
      }
    }
    this.cache.set(key, value);
    this.order.push(key);
  }

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