-
Notifications
You must be signed in to change notification settings - Fork 597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generating Narrower Types #1538
Comments
Eventually, it'd be nice to focus on the story around genericization as well. In certain cases, users may want to supply their own types. DynamoDB document clients, for instance, could accept an import "@aws-sdk/types/strict"; // module augmentations
import {DocDBClient} from "@aws-sdk/client-docdb";
interface Todo {
id: string;
title: string;
body: string;
}
// thanks to the augmentation, we'd be able to specify this generic type param
const client = new DocDBClient<Todo>({
region: "us-west-2",
}); In the rough example above, passing |
Also, can someone possibly explain the generated ...
- SomeField: SomeValue | undefined;
+ SomeField?: SomeValue;
... |
This was done for future-proofing the code, for cases in which required parameters and changed to optional in service types. Details in #1124 (comment) [EDIT] |
I don't feel as though that's the right approach to such future-proofing, as it doesn't take into account other changes to service types (like string -> number, etc.). Nonetheless, how's this for a solution: we could make stricter types available though module augmentation. import "@aws-sdk/types/strict"; |
Changing the type is not allowed under backward compatibility rules. |
How are stricter types expected to work for specific operations? import { RespondToAuthChallengeCommand } from "@aws-sdk/client-cognito-identity-provider"; |
declare module "@aws-sdk/client-cognito-identity-provider" {
export type RespondToAuthChallengeCommand = NarrowedRespondToAuthChallengeCommand;
} Just to reiterate... The generated The augmentation would be applied with the following import statement. import "@aws-sdk/types/strict/client-cognito-identity-provider"; |
Loosely (and without the extras): class RespondToAuthChallengeCommand<
Input extends RespondToAuthChallengeCommandInput,
Output extends RespondToAuthChallengeCommandOutput<Input>
> extends $Command<Input, Output> {
constructor(input: Input) {}
} |
Greetings! We’re closing this issue because it has been open a long time and hasn’t been updated in a while and may not be getting the attention it deserves. We encourage you to check if this is still an issue in the latest release and if you find that this is still a problem, please feel free to comment or open a new issue. |
Commenting so the actions bot doesn't close this out, as I still believe this is an important issue. Hope all is well! |
Greetings! We’re closing this issue because it has been open a long time and hasn’t been updated in a while and may not be getting the attention it deserves. We encourage you to check if this is still an issue in the latest release and if you find that this is still a problem, please feel free to comment or open a new issue. |
Over two years and still no narrowing? How is this possible? Why is there no emphasis on offering a more-narrowly-typed (and therefore safer) DX. Quite disappointing. For those reading this issue, also disappointed: check out typesafe-dynamodb and franken-srp. |
I definitely support at least the option of being able to use stricter types. Upgrading from V2, I was surprised to find that APIs i was using before, like ParameterStore did not have source direct replacements, and instead, I have to use APIs with types that force me to use postfix I guess if AWS isn't going to fix this, it might just be on the community to write a wrapper with accurate types. |
Ran into this today with the union types. Something like MediaFormat or Subtitles.Formats have a clear list of options in the docs, but are just listed as a string in the types. Only once you run the code do you get errors like would it not make sense to have these values as a string union instead of just a string? |
partially addressed in #5356, which closed string unions to only the enumerated values. |
The service models from which we generate our types do not contain enough information to produce the inference types. We cannot implement that part of the feature request practically. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread. |
Is your feature request related to a problem? Please describe.
The smithy-generated TypeScript types can be safer and more inference-producing. For example:
Cognito User Pool's
IntiateAuth
command accounts for many auth flows (represented by theAuthFlowType
enum). Based on that supplied enum, we can discriminate betweenAuthParameters
to type-check flow-specific input fields. We can also return the correct flow-specific outputs (narrowedChallengeName
,ChallengeParameters
,AuthenticationResult
(its omission), etc.).For the
RespondToAuthChallenge
command, here are some of the varying signatures we might get (currently untyped).Describe the solution you'd like
Let's discuss the approach to smithy-model-to-ts-type codegen. Ideally, we could provide more information, not present in the Smithy models. This info could enable inference of the correct return types based on inputs.
[EDIT]:
If we could specify the input-specific output types like this (for
InitiateAuth
, in this example):We could generate a generic output type such as this:
The InitiateAuth command can keep a generic reference to the constructor param, and pass the narrowed type into
$Command
.The caveat is that the input params need to be narrowed (readonly), but it's still a huge DX improvement for TypeScript power users, and would enable us to safeguard––at the type-level––against all kinds of misuse.
Additional context
To avoid breaking backwards-compat, we could expose the narrowed types as a set of module augmentations, available in
@aws-sdk/types/strict.ts
.[EDIT]: moved semi-related (but probably for-now unproductive snippet to comment below)
In Summary
I believe stricter types are essential to customer success, as they serve both in protecting against misuse and ease of experience.
Feedback would be greatly appreciated!
The text was updated successfully, but these errors were encountered: