classes-2-class-alias-ctor-effect.ts 753 B

1234567891011121314151617
  1. // A class aliased through a plain variable (`const Alias = Cfg`) and new'd through the
  2. // alias. The constructor's SIDE EFFECT (writing the static field Cfg.last) is the only
  3. // carrier: the instance value returned by `new Alias(secret)` is never used, so
  4. // by-value arg folding cannot mask a missed constructor application.
  5. // Runtime: new Alias(secret) runs the ctor => Cfg.last = secret => feeds ask#2.
  6. // Expected: source -> ask#1, ask#1 -> ask#2 (ctor param -> Cfg.last), ask#2 -> sink.
  7. const secret = await agent("writer").ask<string>("secret");
  8. class Cfg {
  9. static last = "";
  10. constructor(x: string) {
  11. Cfg.last = x;
  12. }
  13. }
  14. const Alias = Cfg;
  15. new Alias(secret);
  16. const out = await agent("reader").ask<string>(`use ${Cfg.last}`);
  17. return out;