Skip to content

Instantly share code, notes, and snippets.

View chenyong's full-sized avatar
💭
Debugging TypeScript...

ChenYong chenyong

💭
Debugging TypeScript...
  • NIO
  • Shanghai
View GitHub Profile
@chenyong
chenyong / brew-list
Created November 7, 2018 10:28
Current brew list
=>> brew list
ack gettext libidn2 nginx python
adns ghc libksba node@10 python3
aspcud git libpng npth python@2
bash gmp libtasn1 ocaml readline
bash-completion gnupg libtiff ocamlbuild rlwrap
camlp4 gnutls libtool opam ruby
clasp go libunistring openssl sphinx-doc
clingo graphviz libusb [email protected] sqlite
clojure gringo libyaml p11-kit tcpflow
@chenyong
chenyong / safe-prop.ts
Last active December 10, 2018 13:32
A stupid Optional Chaining alternative in TypeScript, demo https://streamable.com/vnyap
// get
export function safeGet<T, K1 extends keyof T>(x: T, k1: K1): T[K1] {
if (x != null) {
return x[k1];
}
return null;
}
export function safeGet2<T, K1 extends keyof T, K2 extends keyof (T[K1])>(x: T, k1: K1, k2: K2) {
@chenyong
chenyong / immer-helpers.ts
Created December 10, 2018 11:47
Helper functions to use Immer in React projects.
import produce from "immer";
export interface ImmerStateFunc<S> {
(f: ((s: S) => void)): Promise<any>;
}
export interface MergeStateFunc<S> {
(partialState: Partial<S>): Promise<any>;
}
if (symbol.flags & (32 /* Class */ | 64 /* Interface */)) {
if (symbol.valueDeclaration && symbol.valueDeclaration.parent == null) {
console.log(node) <---- log
}
if (symbol.valueDeclaration && ts.isBinaryExpression(symbol.valueDeclaration.parent)) {
var jsdocType = getJSDocTypeReference(node, symbol, typeArguments);
if (jsdocType) {
return jsdocType;
}
}
@chenyong
chenyong / ty-errors.text
Created February 20, 2019 03:22
Some type errors found in upgrading ant typescript
✖ 「atl」: Checking finished with 31 errors
[at-loader] ../node_modules/@types/react-router/index.d.ts:104:55
TS2344: Type 'keyof T' does not satisfy the constraint 'string'.
Type 'string | number | symbol' is not assignable to type 'string'.
Type 'number' is not assignable to type 'string'.
[at-loader] ../node_modules/@types/react-router-dom/index.d.ts:15:5
TS2305: Module '"../../../../../../../Users/chen/work/web/node_modules/@types/react-router"' has no exported member 'generatePath'.
[at-loader] ./ts/report/models/component.ts:55:23
@chenyong
chenyong / used-icons
Created February 27, 2019 07:48
Icons really used in antd
check-circle
check-circle-o
close-circle
close-circle-o
default
down
exclamation-circle
exclamation-circle-o
info-circle
@chenyong
chenyong / return.ts
Created May 20, 2019 06:48
Infer return type from function parameter's type, based on https://dev.to/aexol/typescript-tutorial-infer-keyword-2cn This is far from satisfaction.
let a ={
b: {
c:1
}
}
type A = typeof a
@chenyong
chenyong / check.cljs
Created June 3, 2019 03:28
Comparing Clojure spec and TypeScript with multiple structs
(def data {
:mock-all? false
:mocks [
{:path "api/test/json", :type :json :status 200 :data {"code" 200, "message" "This is a test json message in EDN"}}
{:path "api/test/file", :type :file :status 200 :file "user-info.json"}
{:path "api/test/text", :type :text :status 200 :text "user-info.json"}
]
:sites {
"api" {:target :apis}
"web" {:target :proxy, :path "/web", :host "http://localhost:8200"}
@chenyong
chenyong / markup.tsx
Created June 25, 2019 11:21
Immer Form solution
<LabeledField label={lingual.event} showRequired={false}>
<EventDropdown
placeholder={lingual.pleaseSelect}
notFoundContent={lingual.pleaseSelect}
onDeselect={() => {
this.changeField("event", null);
}}
value={form.event}
onChange={(value) => {
this.changeField("event", value);
@chenyong
chenyong / dropdown-area.tsx
Created September 3, 2019 06:49
Dropdown area component with mostly DOM operations. It works but has quite some drawbacks.
import { h, FunctionComponent, VNode, render } from "preact";
import { css } from "emotion";
import { useState, useRef, useEffect, useLayoutEffect } from "preact/hooks";
import { timeout } from "../util/time";
interface IPosition {
left?: number;
right?: number;
top?: number;
bottom?: number;