Skip to content

Instantly share code, notes, and snippets.

View HerringtonDarkholme's full-sized avatar

Herrington Darkholme HerringtonDarkholme

View GitHub Profile
# I am comment
# Basic Data type
123 # int
12.3 # float
6.0221409e+23 # scentific notation
true # bool
"string"
# String Operation
@HerringtonDarkholme
HerringtonDarkholme / commutative.ts
Created December 3, 2020 06:47
Example for comutative intersection type
// https://www.typescriptlang.org/play?ts=4.1.2#code/C4TwDgpgBAsg9gEwgGwDwwHxQLxQN4BQUxUwEAzsAFxQCMATAMwEC+UAZLAaJFAII58AOwgB3ABQBKGvCRo8AQxoNGLDC27hoAIUF4RE6bEQpUAVyEBrIXFFD1m3gGFBAztsfQAIoN2c+BARIAMbICgBO0ABuEVDBNC4EMeFQCoIGcQQKAHRp7Jw5ZJRBEKER0bEIND5JsQBG6WJQCAR1uRycbUXAQA
type Model<M> = {
test: 123
} & M
type A = {new(): Model<{a: 123}>}
type B = {new(): Model<unknown>}
type C = A & B
type D = B & A
@HerringtonDarkholme
HerringtonDarkholme / index.html
Last active March 18, 2020 16:54
PreventDefault in Web Worker
<a class="worker" href="javascript:alert('not prevented')">preventDefault works!</a>
<a class="non-worker" href="javascript:alert('not prevented')">preventDefault doesn't work...</a>
<script>
function createWorker(fn) {
const blob = new Blob([`(${fn.toString()})()`], {type: 'application/javascript'})
const url = URL.createObjectURL(blob)
return new Worker(url)
}
const WAITING = 0
@HerringtonDarkholme
HerringtonDarkholme / get-types.ts
Last active September 17, 2019 19:01
A demonstration of variadic generic in TS3.0
/**
* This gist demonstrates how powerful TypeScript's new tuple type is.
* Say, we want to write a function that receive multiple arguments, and return a tuple of arguments' types.
* For example, `getTypes(1, true, 'str')` return a tuple `['number', 'boolean', 'string']`.
* We can do that in **type safe** way in TypeScript now! And actually we can do the computation at compile time!
* This is so like dependent type in TypeScript!
* Reference: https://github.com/Microsoft/TypeScript/pull/24897
*/
@HerringtonDarkholme
HerringtonDarkholme / VSCode error
Created April 6, 2018 07:52
TypeScript Error reproduciton
Error processing request. Cannot set property 'typeParameters' of undefined
TypeError: Cannot set property 'typeParameters' of undefined
at assignContextualParameterTypes (/project/node_modules/typescript/lib/tsserver.js:36049:38)
at checkFunctionExpressionOrObjectLiteralMethod (/project/node_modules/typescript/lib/tsserver.js:36336:29)
at checkObjectLiteralMethod (/project/node_modules/typescript/lib/tsserver.js:37103:38)
at checkObjectLiteral (/project/node_modules/typescript/lib/tsserver.js:33798:32)
at checkExpressionWorker (/project/node_modules/typescript/lib/tsserver.js:37192:28)
at checkExpression (/project/node_modules/typescript/lib/tsserver.js:37144:42)
at checkExpressionForMutableLocation (/project/node_modules/typescript/lib/tsserver.js:37088:24)
at checkPropertyAssignment (/project/node_modules/typescript/lib/tsserver.js:37096:20)
@HerringtonDarkholme
HerringtonDarkholme / example.json
Created March 20, 2017 10:34
example vscode response
{"jsonrpc":"2.0","id":2,"result":{"isIncomplete":false,"items":[{"label":"aria-activedescendant","kind":12,"textEdit":{"range":{"start":{"line":3,"character":13},"end":{"line":3,"character":14}},"newText":"aria-activedescendant=\"$1\""},"insertTextFormat":2},{"label":"aria-atomic","kind":12,"textEdit":{"range":{"start":{"line":3,"character":13},"end":{"line":3,"character":14}},"newText":"aria-atomic=\"$1\""},"insertTextFormat":2},{"label":"aria-autocomplete","kind":12,"textEdit":{"range":{"start":{"line":3,"character":13},"end":{"line":3,"character":14}},"newText":"aria-autocomplete=\"$1\""},"insertTextFormat":2},{"label":"aria-busy","kind":12,"textEdit":{"range":{"start":{"line":3,"character":13},"end":{"line":3,"character":14}},"newText":"aria-busy=\"$1\""},"insertTextFormat":2},{"label":"aria-checked","kind":12,"textEdit":{"range":{"start":{"line":3,"character":13},"end":{"line":3,"character":14}},"newText":"aria-checked=\"$1\""},"insertTextFormat":2},{"label":"aria-colcount","kind":12,"textEdit":{"range":
@HerringtonDarkholme
HerringtonDarkholme / wtf.ts
Last active November 22, 2016 16:23
WTF have I written?
export type HTML = {
div: IfTag<HTML, 'div'>
p: IfTag<HTML, 'p'>
ul: UL<HTML>
ol: OL<HTML>
img: () => HTML
}
export type Comp<Parent, End extends string> = {
props(prop: {[k: string]: any}): Comp<Parent, End>
@HerringtonDarkholme
HerringtonDarkholme / 1.ts
Last active November 21, 2016 10:06
Translate vue template into hand write template code
h('div', {class: 'el-autocomplete', directive: ['clickoutside']}, [
h('el-input', { props: {
value: this.value,
disabled: this.disabled,
placeholder: this.placeholder,
name: this.name,
size: this.size,
}, on: {
change: this.handleChange.bind(this),
focus: this.handleFocus.bind(this),
@HerringtonDarkholme
HerringtonDarkholme / ctrlp.vim
Last active July 6, 2019 17:48
give syntax highlight for ctrlp vim-devicon
"I borrowed this crazy code from vim-tomorrow-theme colorschemes
" Returns an approximate grey index for the given grey level
fun! s:grey_number(x)
if &t_Co == 88
if a:x < 23
return 0
elseif a:x < 69
return 1
elseif a:x < 103
@HerringtonDarkholme
HerringtonDarkholme / vue.vim
Last active October 3, 2016 16:42
vue syntax highlighting
" Vim syntax file
" Language: Vue.js
" Maintainer: Eduardo San Martin Morote
if exists("b:current_syntax")
finish
endif
if !exists("s:syntaxes")
" Search available syntax files.