Skip to content

Instantly share code, notes, and snippets.

@andremw
Last active June 7, 2022 01:03
Show Gist options
  • Save andremw/16d11720b555f7d2d693d56fe87d2080 to your computer and use it in GitHub Desktop.
Save andremw/16d11720b555f7d2d693d56fe87d2080 to your computer and use it in GitHub Desktop.
My TypeScript snippets for VS Code
{
"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}",
"};"
]
}
}
@andremw
Copy link
Author

andremw commented Jun 7, 2022

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