Last active
June 7, 2022 01:03
-
-
Save andremw/16d11720b555f7d2d693d56fe87d2080 to your computer and use it in GitHub Desktop.
My TypeScript snippets for VS Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Create a nominal type": { | |
"prefix": "nominal", | |
"body": "type $1 = Nominal<$0, '$1'>;", | |
"description": "Creates a nominal type." | |
}, | |
"Define the base Nominal type": { | |
"prefix": "basenominal", | |
"body": [ | |
"class Tagged<T> { #secret_tag: T }", | |
"type Nominal<Type, Tag> = Type & Tagged<Tag>;" | |
], | |
"description": "Creates the base Nominal type that will be used to create other types from it." | |
}, | |
"Create a branded type": { | |
"prefix": "branded", | |
"body": [ | |
"type $1 = {", | |
"\t_brand: '_$1';", | |
"\t$0;", | |
"};" | |
] | |
}, | |
"Create a constrained type": { | |
"prefix": "constrained", | |
"body": [ | |
"interface $1 {", | |
"\treadonly _tag: '$1'", | |
"\tcreate(value: $0): $1", | |
"\tvalue(val: $1): $0", | |
"}", | |
"", | |
"class $1 implements $1 {", | |
"\tprivate internalValue: $0;", | |
"", | |
"\tprivate constructor(value: $0) {", | |
"\t\tthis.internalValue = value;", | |
"\t}", | |
"", | |
"\tstatic create(value: $0): $1 {", | |
"\t\treturn new $1(value);", | |
"\t}", | |
"", | |
"\tstatic value(val: $1) {", | |
"\t\treturn val.internalValue", | |
"\t}", | |
"};" | |
] | |
} | |
} |
I use Rescript now, forget about tS
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some nice ideas taken from https://learnfromit.co/functionaltypescript/2020/03/26/domain-modeling-made-functional-with.html but I need to experiment more