Skip to content

Instantly share code, notes, and snippets.

View adarshaacharya's full-sized avatar
🏠
Working from home

Aadarsha Acharya adarshaacharya

🏠
Working from home
View GitHub Profile
@adarshaacharya
adarshaacharya / Every possible TypeScript type.md
Created September 26, 2021 01:30 — forked from laughinghan/Every possible TypeScript type.md
Diagram of every possible TypeScript type

Hasse diagram of every possible TypeScript type

  • any: magic, ill-behaved type that acts like a combination of never (the proper [bottom type]) and unknown (the proper [top type])
    • Anything except never is assignable to any, and any is assignable to anything at all.
    • Identities: any & AnyTypeExpression = any, any | AnyTypeExpression = any
    • Key TypeScript feature that allows for [gradual typing].
  • unknown: proper, well-behaved [top type]
    • Anything at all is assignable to unknown. unknown is only assignable to itself (unknown) and any.
    • Identities: unknown & AnyTypeExpression = AnyTypeExpression, unknown | AnyTypeExpression = unknown
  • Prefer over any whenever possible. Anywhere in well-typed code you're tempted to use any, you probably want unknown.
@adarshaacharya
adarshaacharya / enum-to-list.ts
Created June 28, 2021 18:04
convert enums into list of arrays
/**
* convert enums into list of arrays
*/
enum UserRole {
Student = 'STUDENT',
Teacher = 'TEACHER',
}
const list = (enm: Record<string, unknown>): string[] => {
@adarshaacharya
adarshaacharya / storage.ts
Created May 22, 2021 05:35
modified credentials options for sheetsql.
import { google, sheets_v4 } from 'googleapis'
import * as _ from 'lodash'
import { Errors } from './errors'
import { Query, Document } from './types'
import * as bluebird from 'bluebird'
export interface IStorage {
insert(docs: Document[]): Promise<Document[]>
find(query?: Query): Promise<Document[]>
update(
@adarshaacharya
adarshaacharya / react-hook-form-v6.tsx
Created May 13, 2021 07:19
react-hook-form v6 simple example
import { useState } from 'react';
import { SubmitHandler, useForm } from 'react-hook-form';
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import { isLoggedInVar } from '../apollo';
type Inputs = {
email: string;
password: string;
};
@adarshaacharya
adarshaacharya / LoggedOutRouter.tsx
Created May 13, 2021 05:14
Handle large form in react in easy way
import { useState } from 'react';
export const LoggedOutRouter = () => {
const [values, setValues] = useState({
email: '',
password: '',
});
const handleSubmit = (e: any) => {
// declare custom types in types/express/index.d.ts
declare namespace Express {
interface Response {
// result middleware
paginatedResults: {
previous: {
page: number;
limit: number;
};
next: {
@adarshaacharya
adarshaacharya / use-form.ts
Created August 20, 2020 18:08
Custom hooks to handle the value change in form inputs.
import React from 'react';
type Props = {
title?: string;
};
type TReturn = [Props, (event: React.FormEvent<HTMLInputElement>) => void];
const useForm = (initialVal: Props): TReturn => {
const [formData, setFormData] = React.useState(initialVal || {});