```ts
/**
 * A fixed-capacity least-recently-used cache implementation.
 */
export class LruCache<K, V> {
  private capacity: number;
  private map: Map<K, V>;

  /**
   * Constructs an LRU cache with the specified capacity.
   * @param capacity The maximum number of entries the cache can hold.
   * @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 for the given key, or undefined if not found.
   * Accessing an existing key moves it to most recently used position.
   * @param key The key to look up.
   * @returns The value associated with the key, or undefined if not found.
   */
  get(key: K): V | undefined {
    if (!this.map.has(key)) {
      return undefined;
    }
    const value = this.map.get(key);
    // Remove and re-set to move to most recently used position
    this.map.delete(key);
    this.map.set(key, value as V);
    return value as V;
  }

  /**
   * Inserts or updates a key-value pair in the cache.
   * If the cache is at capacity, evicts the least recently used entry first.
   * @param key The key to insert or update.
   * @param value The value to associate with the key.
   */
  set(key: K, value: V): void {
    if (this.map.has(key)) {
      this.map.delete(key);
    } else if (this.map.size >= this.capacity) {
      // Evict least recently used entry (first item in Map)
      const lruKey = this.map.keys().next().value;
      if (lruKey !== undefined) {
        this.map.delete(lruKey);
      }
    }
    this.map.set(key, value);
  }

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