Define type which references value of keys of items within array

This is long and confusing, sorry. I’m modeling a large application’s architecture in TypeScript, consider the following layout:

Definition

enum ProjectKey {
  Example1 = "example1",
}

class Project<TProjectKey extends ProjectKey, TApps extends App<any, any>[]> {
  constructor(public key: TProjectKey, public apps: TApps) { }
}

class App<TName extends string, TSecrets extends Secrets = never> {
  constructor(public name: TName, public port: number, public secrets?: TSecrets = undefined){ }
}

// boolean param is whether or not the secret is required.
type Secrets = Record<string, boolean>;

Implementation

type Example1Apps = [
  App<"app1", App1Secrets>,
  App<"app2">,
];

type App1Secrets = {
  username: true;
  password: true;
  optionalSecret: false;
};

const Example1 = new Project<ProjectKey.Example1, Example1Apps>(ProjectKey.Example1, [
  new App("app1", 1234, {
    username: true,
    password: true,
    optionalSecret: false,
  }),
  new App("app2", 5678),
]);

type AllProjects = {
  [ ProjectKey.Example1 ]: typeof Example1,
};

You can see that the goal here is to define a structure of (many) projects, their apps, and each app’s configuration. It creates some redundancy, sure, but the goal is strong-typing. This strongly-typed config structure is then used to build a configuration JSON which matches the structure.

For example, the config would need to define example1.app1.secrets.username = "...", but it cannot include a non-existent app or property, e.g. example1.app6... and example1.app1.secrets.someOtherSecret are invalid.

Configuration Definition

The config would look like this, and would be strongly typed:

type ClusterConfigProps = {
  [ TProjectKey in keyof AllProjects ]: ProjectConfigProps<TProjectKey>;
};

type ProjectConfigProps<TProjectKey extends ProjectKey> = {
  apps: {
    [ TApp in AllProjects[TProjectKey]["apps"][number]["name"] ]: AppConfigProps<TProjectKey, TApp>;
  };
};

// Below, how to get the app within "apps" whose name is TApp?
type AppConfigProps<TProjectKey extends ProjectKey, TApp extends AllProjects[TProjectKey]["apps"][{ name: TApp }] =
  AllProjects[TProjectKey]["apps"][{ name: TApp }] extends { secrets: infer TSecrets }
    ? AppSecretsConfigProps<TSecrets>
    : never;

type AppSecretsConfigProps<TSecrets extends Secrets> =
  TSecrets extends undefined
  ? undefined
  : {
    [ TSecretKey in keyof TSecrets ]: TSecrets[TSecretKey] extends true
    ? { encryptedValue: string }
    : { encryptedValue: string? }
  };

Configuration Implementation

const config: ClusterConfigProps = {
  example1: {
    app1: {
      secrets: {
        username: "abcd",
        password: "passw0rd",

        // This one is optional, can be included or not.
        optionalSecret: "shhh, it's optional",
      },

    // Note that app2 has no secrets (or other config), so it shouldn't be supplied.
  },

  // Other "projects"...
  }
};

See it here in the TS Playground.

The main question here is how can I constrain an array’s items by some value of some key? Like I’ve attempted to do here … to “select” only the app whose name is the TApp generic variable.

type X = AllProjects[TProjectKey]["apps"][{ name: TApp }]

So I would exepect:

type X = AllProjects[ProjectKey.Example1]["apps"][{ name: "app1" }]["secrets"];

To be:

"username" | "password" | "optionalSecret"

Is it possible?

20

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật