TypeScript static classes

后端 未结 12 718
太阳男子
太阳男子 2020-12-04 13:34

I wanted to move to TypeScript from traditional JS because I like the C#-like syntax. My problem is that I can\'t find out how to declare static classes in TypeScript.

12条回答
  •  北荒
    北荒 (楼主)
    2020-12-04 14:17

    With ES6 external modules this can be achieved like so:

    // privately scoped array
    let arr = [];
    
    export let ArrayModule = {
        add: x => arr.push(x),
        print: () => console.log(arr),
    }
    

    This prevents the use of internal modules and namespaces which is considered bad practice by TSLint [1] [2], allows private and public scoping and prevents the initialisation of unwanted class objects.

提交回复
热议问题