Skip to content

Instantly share code, notes, and snippets.

Avatar

Marcel Mokoš marcelmokos

View GitHub Profile
View axiosInstance.ts
import axios from "axios";
import {history} from "../helpers/history";
import {
makeAuthorizationHeader,
isTokenValid,
getToken,
setToken,
tokenIsExpired,
} from "../helpers/token";
View article.js
const article = {
"id": "a2f693f1-e63c-4190-81b3-7a0b459a2517",
"author": {
"id": "6e9418c3-8658-466f-81d6-58ac5548d3dd",
"displayName": "Gene Price",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/iamsteffen/128.jpg",
"color": "#486071",
"url": "author/Gene-Price_6e9418c3-8658-466f-81d6-58ac5548d3dd"
},
"title": "at odit nulla",
View propTypes-isRequired.js
Component.propTypes = {
requiredArray: PropTypes.array.isRequired,
requiredBool: PropTypes.bool.isRequired,
requiredFunc: PropTypes.func.isRequired,
requiredNumber: PropTypes.number.isRequired,
requiredObject: PropTypes.object.isRequired,
requiredString: PropTypes.string.isRequired,
requiredSymbol: PropTypes.symbol.isRequired,
// ...
};
View propTypes.js
Component.propTypes = {
optionalArray: PropTypes.array,
optionalBool: PropTypes.bool,
optionalFunc: PropTypes.func,
optionalNumber: PropTypes.number,
optionalObject: PropTypes.object,
optionalString: PropTypes.string,
optionalSymbol: PropTypes.symbol,
// ...
};
View typed-object-argument-function.ts
const fullName = (
{firstName = "", lastName = ""} // we are providing default values
: {firstName: string, lastName: string} // type, can be inferenced
= {} // default value object for object argument
) => `${firstName} ${lastName}`;
@marcelmokos
marcelmokos / functions.ts
Created Oct 16, 2019
typing typescript functions
View functions.ts
function add(a: number, b: number): number { return a + b };
const add = (a: number, b: number): number => a + b;
type Arithmetics = (a: number, b: number) => number;
// or
/*
interface Arithmetics {
(a: number, b: number): number;
}
@marcelmokos
marcelmokos / typescript-basic-types-table.csv
Last active Mar 22, 2022
Typescript Basic Types table
View typescript-basic-types-table.csv
Javascript type constructor example values typescript type example usage alternative syntax
Boolean Boolean(1); true, false boolean const isOpen: boolean = true;
Number Number('1'); 1, 2 number const n: number = 1;
String String('');, '',", `` 'ok' string const str: string = 'ok';
Array new Array(); []; [1, 2, 3]; number[]; Array<number>; const list: number[] = [1, 2, 3]; const list: Array<number> = [1, 2, 3];
Null null null null const n: null = null;
Undefined undefined undefined undefined const u: undefined = undefined;
Object new Object(); {key: 'value'}; object const item: object = {key: 'value'}; const item: {[key: string]: string} = {key: 'value'};
['Hello', true]; Touple const hi: [string, boolean] = ['Hello', true]
enum enum Color {Red, Green, Blue}
View jsdoc.js
/**
* A person object with name, age and sayName method.
* @typedef {Object} Person
* @property {string} name The person's name.
* @property {number} age The person's age.
* @property {Function} sayName A function that alerts the person's name.
*/
const person = {
name: 'Joe',
age: 32,
View Mobx-example.js
import React from "react";
import {inject, observer} from "mobx-react";
export class SubjectsPresenter extends React.Component {...}
export default inject(
({subjectStore, translator}, {subjectSelections, ...props}) => {
const subjectOptions = subjectStore.getFilteredSubjects(subjectSelections);
return {
subjectOptions,
View prompt.js
// see types of prompts:
// https://github.com/enquirer/enquirer/tree/master/examples
//
module.exports = [
{
type: "input",
name: "name",
message: "What's the name of your package?"
},
{