typescript
typescript copied to clipboard
Support for variable resolution pattern from Serverless
Use case description
Region is defined as a union of string literals which works fine if the region is being implicitly set, however when attempting to use Serverless variables a type error is raised.
The following is valid:
const serverlessConfiguration: AWS = {
provider: {
name: "aws",
runtime: "nodejs12.x",
region: "us-east-1",
},
}
The following is considered invalid:
const serverlessConfiguration: AWS = {
provider: {
name: "aws",
runtime: "nodejs12.x",
region: "${opt:region, 'us-east-1'}",
},
}
Proposed Solution
Add string type to the end of the union type for region to address this issue. The tradeoff being we lose type safety but keep auto completion and fix the issue of variables being used for region. I'm open to any/all other proposed solutions if anyone has any ideas.
Hi @ryansonshine, thanks for reporting this issue.
Your solution is fine with me. This should be done in serverless/typescript and not in serverless/serverless since the framework will actually resolve "${opt:region, 'us-east-1'}" before proceeding with JSON-schema validation.
The definitions of this repository are programmatically generated from JSON-schema definitions contained in serverless/serverless. In order to make this modification - which does not only affects the region keyword, but all enums - we should implement a custom post-compiler to add the last union with a string type.
At the moment, json-schema-to-typescript does not offer such option. I'm working on switching to quicktype - see #1 - and should have it implemented this week. Quicktype offers much wider configuration option, with the ability to implement custom converters. One of those could be parsing JSON-schema enums containing only strings to add this last union. WDYT ?
Sounds good to me, subscribing to #1 to track progress. I'll implement the converter to cover this bug after the switch is made.
Thanks!
any update when this will be resolved?
@pjain11, this issue is blocked by #1 . It will be resolved shortly after
casting the command line argument to AWS["provider"]["region"] helped to fix this issue for me.
region: "${opt:region, 'us-east-1'}" as AWS["provider"]["region"],
Thx you, @ganimp84