Aws-sdk-js: Typescript (and Angular2 support).

Created on 17 May 2016  路  15Comments  路  Source: aws/aws-sdk-js

Would like to request a feature, typings for typescript. I see there are some repo's out there, but none are official. It would be nice for those of us moving to Angular2 and typescript in general.

feature-request

Most helpful comment

@EmmanuelTsouris
We've actually been doing some work on generating typescript definition files for our SDK. It's not 100% complete yet, but I can add a typings directory with what's done so far. Would love to get some feedback on it.

I'll add that in this week.

All 15 comments

@EmmanuelTsouris
We've actually been doing some work on generating typescript definition files for our SDK. It's not 100% complete yet, but I can add a typings directory with what's done so far. Would love to get some feedback on it.

I'll add that in this week.

@chrisradek that would be awesome thanks!

@EmmanuelTsouris
Sorry for the delay, had to make a few changes to get the definitions in a workable state.

They are still very much a work in progress, but I created a branch you can checkout here:
https://github.com/aws/aws-sdk-js/tree/support/typescript

I'm working on automating the task of generating the typescript definition files (most of the definitions are generated based on the api models). Once that's done we'll be able to keep these up to date with each release.

Please share any feedback you have!

@chrisradek I started playing around with it. I created a new Angular2 app using Angular-CLI. Then I imported your branch (support/typescript) in my package.json

"aws-sdk": "git:github.com/aws/aws-sdk-js#support/typescript"

In one of my *.component.ts files, I imported the sdk and the typings were picked up.

import * as AWS from 'aws-sdk';

So far adding typed objects seems to be working ... :)

Hi,

I'm trying to use the typing in the way as described by @EmmanuelTsouris, and it's not quite working. When I try to compile a simple project using it, I'm getting a compilation error on line 36:

setPromisesDependency(dep: ()|void): void;

with the following error:

node_modules/aws-sdk/typings/AWS.d.ts(36,32): error TS1005: '=>' expected.

I know it could be a issue with typescript on my machine (as opposed to an issue with the file), but I'm running the most recent version of typescript (as of now @ 1.8.10), so I don't think it's that? I've tried the various target options in tsconfig.json, and none of them seem to support that particular union operator syntax. Though they happily support type x = number | "aa" | "bb"; which makes me thinks it's not an issue with my typescript.

@Nysosis
Thanks for bringing that up. The original intent for adding typings was to enable better code completion support in tools that support that (like vscode, intellij and atom). They are definitely still a work in progress, but this sort of feedback is helpful for improving on it. I think for your particular error, the function type wasn't defined properly, so we should correct that.

@EmmanuelTsouris
Hi, I'm was looking for the aws-sdk's typings and I found https://github.com/ingenieux/aws-sdk-typescript. maybe he could help.

@orioniota Did you get the https://github.com/ingenieux/aws-sdk-typescript repository somehow to run with angular 2?

I came to the point, where I can import the sdk with
import {CognitoIdentity} from "aws-sdk";
without an error.

But at the point where I like to instantiate the CognitoIdentity with
let cgi = new CognitoIdentity()
the browser stops while it is loading the page, with a bunch of errors.

@chrisradek Would be nice to have a official solution for angular 2, cause angular depends on a working aws-sdk to run with aws. Is there a roadmap?

@JanOschii No, I can compile, but I couln't instantiate AWS. I tried many ways.
AWS = window.AWS

@chrisradek So, I attempted to use the branch, and ran into some compile issues (possibly related to the version of TSC?) Specifically:

pnarkinsky-laptop:pureflix-object-cache pnarkinsky$ tsc
node_modules/aws-sdk/typings/AWS.d.ts(36,32): error TS1005: '=>' expected.
pnarkinsky-laptop:pureflix-object-cache pnarkinsky$ tsc
node_modules/aws-sdk/typings/AWS.d.ts(2,24): error TS1147: Import declarations in a namespace cannot reference a module.
node_modules/aws-sdk/typings/AWS.d.ts(3,25): error TS1147: Import declarations in a namespace cannot reference a module.
node_modules/aws-sdk/typings/AWS.d.ts(4,26): error TS1147: Import declarations in a namespace cannot reference a module.
src/mpx-cache.ts(224,13): error TS2345: Argument of type '{ accessKeyId: string; secretAccessKey: string; }' is not assignable to parameter of type 'ConfigurationOptions'.
  Object literal may only specify known properties, and 'accessKeyId' does not exist in type 'ConfigurationOptions'.

I was able to resolve them pretty handily (by modifying the definitions) but wanted to make you aware that it looks like some changes are needed for this to work, at least in my configuration. It would be nice if this could be gotten to the point where it was more "plug and play".

(Disclaimer: I'm new to typescript, so apologies if I'm missing something stupid.)

@fished
Thanks for the feedback! Can you share which version of tsc you're using?

Regarding the last error you received (TS2345), were you specifying the accessKeyId and secretAccessKey in the constructor for AWS.Config or in an AWS.config.update() statement? Depending on which case you're using, that error is intentional, but I'm not sure we clearly model that distinction.

Hello,
I am facing issues using aws-sdk.d.ts file which I have downloaded from github.

import * as AWS from 'aws-sdk';

export function typeScriptViewModel(first, last) {
    var self = this;
    var DataBucket = 'emaildataports';
    var userDatBucket = 'user-data';

    var testBucket = 'emailtests';
    var userTestBucket = 'user--tests';


    AWS.Config({
        accessKeyId: 'myaccessKeyID',
        secretAccessKey: 'mysecretAccessKey'
    });
....
...
...
}

// many more usage of AWS. but got stuck on the first implementation itself.
   //var bucket = new AWS.S3({
   //         params: {
   //             Bucket: bucketName, Prefix: path, Delimiter: '/'
   //         }
   //     });

But when run on the browser, it throws error as
"Uncaught TypeError: Cannot read property 'Config' of undefined".

Can anyone please guide me how to use s3 bucket using aws.sdk.ts? I am new in Typescript.

other usage in the files are like

Anyone who has the solution ?

@chrisradek what about the typings at DefinitelyTyped? Will the typings your working on be distributed with the SDK along with AWS official support or is it a side project? I was working on refactoring to support promises - will you be creating tests for the typings?

@tomdavidson
We're planning on distributing the typings with the SDK. That's being actively worked on here:
https://github.com/aws/aws-sdk-js/pull/1189

This should eliminate the need to pull in the typings from DefinitelyTyped then, and the types will match the version of the SDK that you're using.

We can create tests for the typings, but I'm not sure what coverage we'll shoot for before including them yet.

I have to push some changes to fix some issues the typescript compiler is complaining about, but you're welcome to try out the branch in the PR. We're targeting TypeScript 2.x, I've added a sample tsconfig.json file as a comment in that PR.

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.

Was this page helpful?
0 / 5 - 0 ratings