0

I am trying to convert a typescript file into js, using gulp-typescript.

It doesn't seem to translate.

gulp file:

var ts = require('gulp-typescript');
gulp.task('tsToJs', function () {
    return gulp.src('./config.ts')
      .pipe(ts({
          moduleResolution: 'node',
          target:'es5',
          allowJs:true
          //out: 'config.js'
      }))
      .pipe(gulp.dest('./'));
});

config.ts:

class Config{
    env:{
        readonly live: '.co.uk',
        readonly dev: '.dev.co.uk',
        readonly qa:  'qa.co.uk'
    };
};
export = new Config;

Seems to get converted to this: (It's missing the data inside the class?) conf.js:

"use strict";
var Config = (function () {
    function Config() {
    }
    return Config;
}());
;
module.exports = new Config;
2

2 Answers 2

1

You're declaring your env property to be of an anonymous type, rather than initializing it with an object with properties. Change : to =:

class Config{
    env = {
//      ^-------------------------------------- here
        readonly live: '.co.uk',
        readonly dev: '.dev.co.uk',
        readonly qa:  'qa.co.uk'
    };
}

Note, though, that the playground says that you can't use readonly there. I can't find any example in the TypeScript handbook using readonly on simple object properties. (Though conceptually it would be reasonable.) You may need to be a bit more old-fashioned:

class Config{
    env = Object.create(Object.prototype, {
        live: {value: '.co.uk', enumerable: true, configurable: true},
        dev:  {value: '.dev.co.uk', enumerable: true, configurable: true},
        qa:   {value: 'qa.co.uk', enumerable: true, configurable: true}
    });
}

(Of course, if you don't want the properties to be enumerable or configurable, just leave those off. They won't be writable, because the default is false for all of those flags.)


Side note: Since that's a declaration, not an expression, there's no ; after it. ; is a statement terminator; declarations are self-terminating.

Sign up to request clarification or add additional context in comments.

3 Comments

ok but readonly cannot be used here? That's the error message I get in vscode
@SteveTomlin: I was wondering about that, I couldn't find any example of it being used in that way in the TypeScript handbook. Just noticed in the playground that it says the same thing. I've updated the above.
@JamesMonger: Thank you! My TypeScript-fu is weak. That explains why the block containing labelled statements with commas between them wasn't a problem. It's not a block, and it doesn't contain labelled statements with commas between them. :-)
0

I really wanted to keep the readonly, because it helps vscode display the value of the variables where they are used. There doesn't seem to be any other way of displaying javascript variables without compiling. This feature is the reason I am wanting to use typescript for this situation.

This is the closest to achieving what I want, which does work:

const env = new (class Env{
    readonly live = '.co.uk';
    readonly dev = '.dev.co.uk';
    readonly qa = '.qa.co.uk';
})();

const brand = new (class Brand{
    readonly cm = 'mysite';
})();

export {brand, env};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.