Jump to content

Export a variable from within a class TS

DriedSponge

Is it possible to export a variable in typescript from within a class? For example:
Instead of:

export const variable = "Test";

I could do:
 

class Test{
	constructor(){
		export const variable = "test";
	}
}

Thanks in advance.

Remember to quote or @mention others, so they are notified of your reply

AMD Ryzen 9 5950X | Arctic Liquid Freezer II 360 | MSI B450 TOMAHAWK MAX | G.Skill Trident Z RGB 32 GB DDR4-3600 | Gigabyte GeForce RTX 2080 SUPER 8 GB 

Samsung 980 EVO Plus 2TB | SK hynix Gold S31 500GB SSD | Seagate Barracuda Compute 2 TB 7200RPM HDD | 1TB Samsung 860 EVO SSD | 3x Phanteks T30-120

Corsair RM1000e 1000 W 80+ Gold Certified Modular PSU | Corsair 5000D Airflow Windows 11 Home

Link to comment
Share on other sites

Link to post
Share on other sites

22 minutes ago, Slottr said:

Why?

Trying to eliminate the need for the parseCollection function I have:
 

class SpongeHelpers{
    public this: this;
    constructor(sel) {
        SpongeHelpers.prototype.this = this;
        if (!sel) return;
        if (typeof sel === 'function') {
            document.addEventListener('DOMContentLoaded', sel);
        }else if (typeof sel === 'string') {
            return parseCollection.apply(document.querySelectorAll(sel));
        }

    }
    init(sel){
        return new SpongeHelpers(sel);
    }
}
//var sh;
window['$'] = SpongeHelpers.prototype.init;
const sh = SpongeHelpers.prototype.init;
var parseCollection = function(){
    parseCollection.prototype.this = this;
    return parseCollection.prototype;
};
export const col = parseCollection.prototype;


 

Remember to quote or @mention others, so they are notified of your reply

AMD Ryzen 9 5950X | Arctic Liquid Freezer II 360 | MSI B450 TOMAHAWK MAX | G.Skill Trident Z RGB 32 GB DDR4-3600 | Gigabyte GeForce RTX 2080 SUPER 8 GB 

Samsung 980 EVO Plus 2TB | SK hynix Gold S31 500GB SSD | Seagate Barracuda Compute 2 TB 7200RPM HDD | 1TB Samsung 860 EVO SSD | 3x Phanteks T30-120

Corsair RM1000e 1000 W 80+ Gold Certified Modular PSU | Corsair 5000D Airflow Windows 11 Home

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, DriedSponge said:

Is it possible to export a variable in typescript from within a class? For example:
Instead of:


export const variable = "Test";

I could do:
 


class Test{
	constructor(){
		export const variable = "test";
	}
}

Thanks in advance.

export let variable;

class Test{
	constructor(){
		variable = "test";
	}
}


though this is bad practise you should have a public variable in the class you can access.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, vorticalbox said:

export let variable;

class Test{
	constructor(){
		variable = "test";
	}
}


though this is bad practise you should have a public variable in the class you can access.

I will expierement with this, thank you!

Remember to quote or @mention others, so they are notified of your reply

AMD Ryzen 9 5950X | Arctic Liquid Freezer II 360 | MSI B450 TOMAHAWK MAX | G.Skill Trident Z RGB 32 GB DDR4-3600 | Gigabyte GeForce RTX 2080 SUPER 8 GB 

Samsung 980 EVO Plus 2TB | SK hynix Gold S31 500GB SSD | Seagate Barracuda Compute 2 TB 7200RPM HDD | 1TB Samsung 860 EVO SSD | 3x Phanteks T30-120

Corsair RM1000e 1000 W 80+ Gold Certified Modular PSU | Corsair 5000D Airflow Windows 11 Home

Link to comment
Share on other sites

Link to post
Share on other sites

51 minutes ago, DriedSponge said:

I will expierement with this, thank you!

again mutable exports are not advised.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, vorticalbox said:

again mutable exports are not advised.

Noted 

Remember to quote or @mention others, so they are notified of your reply

AMD Ryzen 9 5950X | Arctic Liquid Freezer II 360 | MSI B450 TOMAHAWK MAX | G.Skill Trident Z RGB 32 GB DDR4-3600 | Gigabyte GeForce RTX 2080 SUPER 8 GB 

Samsung 980 EVO Plus 2TB | SK hynix Gold S31 500GB SSD | Seagate Barracuda Compute 2 TB 7200RPM HDD | 1TB Samsung 860 EVO SSD | 3x Phanteks T30-120

Corsair RM1000e 1000 W 80+ Gold Certified Modular PSU | Corsair 5000D Airflow Windows 11 Home

Link to comment
Share on other sites

Link to post
Share on other sites

You can also make this a public static field on the class, which would be a more idiomatic way to do this I believe

export class Test {
    public static myVar: string = "asdf";
}

It's possible, although generally bad practice, to assign to that from the constructor.

 

You can then use it as

import {Test} from "./test"

Test.myVar // asdf

 

If you want to have a different value per instance of the class, or are able to access it from an instance of the class rather than the class itself, you would not make it static:

export class Test {
    public myVar: string;
    constructor() {
        myVar = "asdf";
    }
}

let test = new Test();
test.myVar; // asdf

 

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×