Skip to content

Instantly share code, notes, and snippets.

@typeofweb
Created June 22, 2021 19:29
Show Gist options
  • Save typeofweb/65f1bdc4505bc7f033a79ff23c6eb950 to your computer and use it in GitHub Desktop.
Save typeofweb/65f1bdc4505bc7f033a79ff23c6eb950 to your computer and use it in GitHub Desktop.
JuniorSenior
import { definitions as Api } from './apitypes';
{
function id1(arg: unknown): unknown {
return arg;
}
function id<Argument>(arg: Argument): Argument {
return arg;
}
function id(arg) {}
type TypeNaszejFunkcji = <Argument>(arg: Argument) => Argument;
const x = 123;
let y = 123;
y = 222;
const obj = {
a: 123,
} as const;
const result = id('adsadsa');
}
{
function Table1<T>(data: T[], renderItem: (item: T) => void) {
return data.map((item) => {
return {
// id: item.id,
view: renderItem(item),
};
});
}
function Table2<Item extends { id: number | string }>(
data: Item[],
renderItem: (item: Item) => void,
) {
return data.map((item) => {
return {
id: item.id,
view: renderItem(item),
};
});
}
const result1 = Table2([{ id: 123 }], () => {});
const result2 = Table2([{ id: 'uuid-123' }], () => {});
}
{
getConfig('NODE_ENV'); // 'production'
getConfig('STRIPE_API_KEY'); // 'dsajkdsja kdljsakldjsakldj sklajd klsajdklsa'
getConfig('PORT'); // 3000
getConfig('DEBUG'); // true
// type Env = {};
interface Env {
NODE_ENV: 'production' | 'staging';
STRIPE_API_KEY: string;
PORT: number;
DEBUG: boolean;
}
const arg = 'PORT';
type X = Env[typeof arg];
function getConfig1(key: keyof Env): Env[typeof key];
const result1 = getConfig1('NODE_ENV');
function getConfig2<Key extends keyof Env>(key: Key): Env[Key];
const result2 = getConfig2('DEBUG');
function getConfig3<Key extends string>(
key: Key,
): Key extends keyof Env ? Env[Key] : string | undefined;
const result3 = getConfig3('DEBUG');
function getConfig4<Key extends keyof Env>(key: Key): Env[Key];
function getConfig4(key: string): string | undefined;
const result4 = getConfig4('PORT');
}
{
// infer
const r = Promise.resolve(123);
type PromiseValue<P> = P extends Promise<infer R> ? R : never;
type Result1 = PromiseValue<typeof r>;
type Awaited<T> = T extends Promise<infer R> ? Awaited<R> : T;
type X = Awaited<Promise<Promise<Promise<number>>>>;
type DeepReadonly<T> = T extends Array<infer R>
? ReadonlyArray<R>
: T extends Map<infer R>
? ReadonlyMap<R>
: T extends object
? {
readonly [Key in keyof T]: DeepReadonly<T[Key]>;
}
: T;
type X2 = DeepReadonly<{
a: number;
c: {
d: string;
};
e: ['a', 'b'];
}>;
{
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
// function doFetch1(method: HttpMethod, path: string): ?{};
// const result1 = doFetch1('GET', '/taxes');
function doFetch2<
Path extends keyof Api['pathsDefinitions'],
Method extends keyof Api['pathsDefinitions'][Path],
>(
path: Path,
method: Method,
): Api['pathsDefinitions'][Path][Method] extends {
response: infer Response;
}
? Response
: null;
const result2 = doFetch2('/orders', 'GET');
}
{
interface User {
id: number;
name: string;
}
function createPlugin<T>(data: T) {}
createPlugin<{
userId: User['id'];
getUsers: () => User[];
}>({
userId: 123,
getUsers: () => [],
});
createPlugin<{
userId: User['id'];
getUsers: () => User[];
}>({
userId: 123,
getUsers: {
cache: '30 seconds',
fn: () => [],
},
});
type AnyFunction = (...args: any[]) => any;
type PluginData<Data> = {
[Key in keyof Data]: Data[Key] extends AnyFunction
? Data[Key] | { cache: CacheStr; fn: Data[Key] }
: Data[Key];
};
function createPlugin2<PluginConfig>(data: PluginData<PluginConfig>) {}
createPlugin2<{
userId: User['id'];
getUsers: () => User[];
}>({
userId: 123,
getUsers: {
cache: '30 minute',
fn: () => [],
},
});
type TimeUnit = 'seconds' | 'minute' | 'hours' | 'days';
type CacheStr = `${number} ${TimeUnit}`;
}
}
{
// DefinitelyTyped
// npm install naszabiblioteka
// npm install @types/naszabiblioteka --save-dev
}
export {};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment