Skip to content

Instantly share code, notes, and snippets.

View eczn's full-sized avatar
Star

eczn* eczn

Star
View GitHub Profile
@eczn
eczn / y-combinator.ts
Created February 5, 2021 09:43 — forked from forivall/y-combinator.ts
Typescript Y-Combinator
// my brain decided to ask the question: yknow, i want you to think about the y combinator --
// like, what is it. like what the fuck girl, cmon, you have a comp sci degree, you should know this, and understand it and shit
// and so i was like fiiiiiiiiiiiiiiine gosh, lets see if typescript can handle the typing, and play around with it
// so i looked up a javascript implementation, and played with the type defintion until it
// matched up and then i was like oh: thats what the type definition of the functions in it are.
// i get it now. that's pretty cool. the main interesting thing is a the inner function that takes itself
// and returns the function initially passed to the outer function. neato.
@eczn
eczn / gzipped-chunked-server.ts
Last active March 15, 2021 07:47
Http Gzipped Chunked Example
import http from 'http';
import zlib from 'zlib';
const wait = (t = 100) => new Promise(r => setTimeout(r, t));
const STR = `<div>${'aaaaaaaaaa'.repeat(500)}</div>`
const server = http.createServer(async (req, res) => {
console.log('ON REQ');
if (req.url !== '/') return res.end();
@eczn
eczn / how-to-inject-node-flag.md
Last active March 23, 2021 15:19
How to inject node flag

bash

$ node <node-flag>

shebang #!

#!/usr/bin/node 
@eczn
eczn / distributive-law-in-conditional-type.md
Last active May 9, 2022 08:33
莲学派 TypeScript 重新理解 never

条件类型中的分配律

TypeScript Conditional Types 条件类型在设计上是 distributable 的, 也就是可以按分配律,也可以不按分配律做处理,具体来说,只有在有泛型参数出现的情况下才会按 distributive 做处理,理由是底层实现如此:

image

而什么是分配律?其实跟乘法的分配律一样,是代数系统一种概念, 如:

type ABABC =
@eczn
eczn / relation-changes-in-type-transforming.md
Last active May 9, 2022 08:38
莲学派 TypeScript 类型变换中的关系变化

类型关系

类型的关系不外乎子母、无关、相等等这类。 一般的,如果 A 是 B 的子类型,则记作 A <= B 那么显然:

  1. 如果 A <= B 且 B <= A 那么 A == B
  2. 如果说 A != B, 则 A 不是 B 的子类型、B 也不是 A 的子类型; 反之亦然; 这类关系揭示了两类型无关的现象

TypeScript 采用了结构化类型,因此很自然地如此理解子类型:

@eczn
eczn / cons-lambda.go
Last active July 2, 2021 07:20
implementation of cons in each language
// golang 纯函数实现
package main
import "fmt"
type unknown = interface{}
type Selector = func(unknown, unknown) unknown
type Cons = func(Selector) unknown
@eczn
eczn / clone.ts
Last active June 26, 2021 17:59
deep clone
type Fn = (...args: unknown[]) => unknown;
// 取出 T 中静态 keys
type StaticKeys<T> = {
[K in keyof T]: T[K] extends Fn ? never : K
}[keyof T]
// 去除 T 里面的函数
type StaticObject<T> = Pick<T, StaticKeys<T>>
@eczn
eczn / README.md
Last active July 20, 2021 10:08
函数逆变
class I1 { a: number = 11 };
class I2 { b: number = 11 };

type K1 = (k: I1) => void;
type K2 = (k: I2) => void;

type K1K2 = K1 | K2;
// TypeScript Version 4.3.5
// 原问题链接:
// https://github.com/type-challenges/type-challenges/blob/master/questions/296-medium-permutation/README.md
// 下面是思考过程:
// 这个问题本质是如何穷举出形如 [1, 2, 3] 的所有组合, 那么如何告诉机器如何穷举呢 ?
type IsUnion<U> = (
// Union A | B 转 Intersection A & B (函参逆变)
(U extends any ? ((args: U) => void) : never) extends ((args: infer R) => void)
// 可理解为 [A | B] extends [A & B] ? false : true
? ([U] extends [R] ? false : true)
: false
)
type T1 = IsUnion<1>; // false
type T4 = IsUnion<[1 | 2]>; // false