1

I am following a tutorial where the instructor uses square brackets after the AWS type when declaring the constant, but I have never seen this before.

import type { AWS } from "@serverless/typescript";

const dynamoResources: AWS["resources"]["Resources"] = {
  urlTable: {
    Type: "AWS::DynamoDB::Table",
    Properties: {
      TableName: "${self:custom.urlTableName}",
      AttributeDefinitions: [
        {
          AttributeName: "id",
          AttributeType: "S",
        },
      ],
      KeySchema: [
        {
          AttributeName: "id",
          KeyType: "HASH", 
        },
      ],
      BillingMode: "PAY_PER_REQUEST",
    },
  },
};

export default dynamoResources;

What are these used for? What is the name for it, so I can read more about it?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Joan
  • 83
  • 1
  • 6
  • 2
    In the same way that `dynamoResources["urlTable"]` would give you the value by its name, `AWS["resources"]` gives you the [_type_](https://github.com/serverless/typescript/blob/ef31dffcd07061f8c0374751e36ebdc019381851/index.d.ts#L1309-L1339) by its name. It's an [indexed access type](https://www.typescriptlang.org/docs/handbook/2/indexed-access-types.html). – jonrsharpe Sep 20 '22 at 14:13
  • In this case it's used to dynamically look up the type instead of defining it in place like e.g. `const name: string = ...`. – Gunnar B. Sep 20 '22 at 14:16
  • thanks so much @jonrsharpe. It makes more sense now. – Joan Sep 20 '22 at 14:18
  • Also, unintuitively, you can't use dot notation; `AWS.resources.Resources` is invalid. I suppose that's because it's reserved for namespaces. – kelsny Sep 20 '22 at 14:23

0 Answers0