2

I am using Angular1 with TypeScript. I have a simple question.

Is there any way to encode-decode a string in base64 with the environment, I use.?

I have searched a lot, I could find this. But I am not sure whether it is available with the Type-definition.

2
  • It doesn't matter that there is no tsd file. You can still use the libary. Either declare it as "any", or create your own simple definition. Commented Sep 8, 2016 at 8:18
  • Thnx for the comment. Sorry I dont get you. can you tell me how can I use that as a service inside typescript? preferably as an answer :)) Commented Sep 8, 2016 at 8:44

1 Answer 1

2

So following the example from https://github.com/ninjatronic/angular-base64 but with a bit of TS decoration:

angular
    .module('myApp', ['base64'])
    .controller('myController', [

        '$base64', '$scope', 
        function($base64: any, $scope : ng.IScope) {

            $scope.encoded = $base64.encode('a string');
            $scope.decoded = $base64.decode('YSBzdHJpbmc=');
    }]);

So all I've done here is said that the $base64 parameter is "any", this allows you to use regular JS against it without TS complaining. You won't get intellisense / type checking against it, but that doesn't matter. As I said in the comment if you do want to define your own interface against it you could. i.e

interface Base64Encode {
    encode: (source: string) => string;  
    decode: (encoded: string) => string;
}
Sign up to request clarification or add additional context in comments.

3 Comments

So, I should be able include your 1st code-snippet inside a .ts file right??
Well this is an example, you will need to fit it into your code, but my point is that it doesn't matter what is injected if you give it a type of any, then TS won't get in the way.
Yes, I understood. Actually I didn't ask that whether I can copy and paste. :)) . I'll update soon after I tried out. :))

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.