```ts
/**
 * LRU (Least Recently Used) cache implementation.
 */
export class LruCache<K, V> {
  /** @private */ private readonly capacity: number;
  /** @private */ private readonly map: Map<K, V>;

  /**
   * Creates an LRU cache with the given capacity.
   * @throws Error if capacity is not a positive integer.
   */
  constructor(capacity: number) {
    if (!Number.isInteger(capacity) || capacity <= 0) {
      throw new Error("Capacity must be a positive integer");
    }
    this.capacity = capacity;
    this.map = new Map<K, V>();
  }

  /**
   * Retrieves the value associated with the key.
   * Counts as a use, moving the entry to most recent position if found.
   */
  get(key: K): V | undefined {
    const hasKey = this.map.has(key);
    if (!hasKey) return undefined;
    const value = this.map.get(key)!; // guaranteed defined
    this.map.delete(key);
    this.map.set(key, value);
    return value;
  }

  /**
   * Inserts or updates the key with the given value.
   * Counts as a use. Evicts least recently used entry if capacity exceeded.
   */
  set(key: K, value: V): void {
    const exists = this.map.has(key);
    if (exists) {
      this.map.delete(key);
    } else if (this.map.size >= this.capacity) {
      // Evict least recently used
      const lruKey = this.map.keys().next().value;
      this.map.delete(lruKey);
    }
    this.map.set(key, value);
  }

  /**
   * Current number of entries in the cache.
   */
  get size(): number {
    return this.map.size;
  }
}
```