Declaration Merging For Typed Contexts

Merge interfaces and namespaces with declaration merging so related types stay in one place and extensions type-check without inheritance.

TypescriptConfig Management

Code

typescriptsnippet
31 lines
interface UserRecord {
  id: string;
  email: string;
}

// declaration merging:同名接口在同一作用域继续追加字段
interface UserRecord {
  tenantId: string;
  roles: readonly string[];
}

namespace Audit {
  export const enabled = true;
}

namespace Audit {
  export function stamp(record: UserRecord): string {
    return record.id + ':' + record.tenantId;
  }
}

const user: UserRecord = {
  id: 'u-1',
  email: 'ada@example.com',
  tenantId: 't-9',
  roles: ['admin', 'billing'],
};

console.log('user:', user.id, user.email, user.tenantId);
console.log('roles:', user.roles.join(','));
console.log('audit enabled:', Audit.enabled, '| stamp:', Audit.stamp(user));

What the snippet does

The snippet is organized around the function stamp.

Using it

The primary signature is export const enabled =

The demo output is labeled (user:, roles:, audit enabled:), so the printed lines read as a per-operation trace of the run.

enabled is what another module would consume; nothing else here is load-bearing for callers.

Key statements

return record.id + ':' + record.tenantId;

Interactive Tools for This Topic

FAQ

Does this require external packages?
No. It relies only on built-in language and platform APIs, so you can paste it into a project without installing anything. The file defines `stamp` at load time.