Skip to content

Instantly share code, notes, and snippets.

Przemyslaw Jan Beigert przemyslawjanpietrzak

Block or report user

Report or block przemyslawjanpietrzak

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View gist:ca91b4b7876cef93e242cea387f87d8c
{
// Place your create-espeo-app workspace snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
// is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
// used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
// Placeholders with the same ids are connected.
// Example:
"Create type safe action": {
"scope": "typescript",
View value-based-types.ts
interface Data {
fn(arg: string): Array<string>
fn(arg: number): null
}
let data: Data;
const a = data.fn(42// null
const b = data.fn("str"// Array<string>
interface API {
View readonly.ts
type ReadonlyObject<A> = { readonly [K in keyof A]: A[K] };
type DeepReadonlyObject<A> = { readonly [K in keyof A]: DeepReadonly<A[K]> }
type X = DeepReadonlyObject<{ key: string, key1: number }>; // { readonly key: any; readonly key1: any; }
View tsc-on-js.js
// @ts-check
export const fn = (arg1, arg2, arg3) => 42;
fn(1, 2, 3, 4); // ERROR Expected 3 arguments, but got 4
document.querySelector(42); // ERROR Argument of type '42' is not assignable to parameter of type 'string'.
/**
*
* @param {string} arg
* @returns {number}
View unknown.ts
const fn = (arg: unknown) => {
arg.x; // ERROR
arg > 3; // ERROR
arg + 1; // ERROR
arg.toString(); // ERROR
arg(); // ERROR
arg === null; // OK
return arg; // OK
}
View chech-browser.js
// https://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser
// Opera 8.0+
var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
// Firefox 1.0+
var isFirefox = typeof InstallTrigger !== 'undefined';
// Safari 3.0+ "[object HTMLElementConstructor]"
var isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || (typeof safari !== 'undefined' && safari.pushNotification));
View property-accessing.ts
interface X {
key1: string;
key2: { id: number, name: string },
key3: number;
}
const fn = (arg: X['key2']) => {
arg.id // number
arg.name // string
};
View tuple.ts
let tuple: [string, number];
tuple = ["hello", 10]; // OK
tuple = [10, "hello"]; // Error
let str = tuple[0]; // string
let num = tuple[1]; // number
View abstract-class.ts
abstract class AbstractClass {
method() {
return 42
}
}
class Class extends AbstractClass {
method1() {
return 42;
}
View strictFunctionTypes.ts
const fn = (arg: number, cb: (string) => string) => {
return 42
}
fn(42, a => a++); // Argument of type '(a: any) => number' is not assignable to parameter of type '(string: any) => string'. Type 'number' is not assignable to type 'string'.
You can’t perform that action at this time.