Skip to content

Instantly share code, notes, and snippets.

@Drag13
Drag13 / advanced-types.ts
Last active November 18, 2021 06:44
TypeScript Advanced Types and Trics
// At least one property
type AtLeastOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> & {
[K in Keys]-?: Required<Pick<T, K>>
}[Keys]
type PerformanceBudget = AtLeastOne<{ fcp: number, lcp: number }>
// Typed Key
[alias]
acp = "!f() { git add . && git commit -m \"$1\" && git push; }; f"
@Drag13
Drag13 / v8-debug-flags
Last active June 21, 2024 19:21
v8 engine flags for DEBUG version v9.4.143
Options:
--abort-on-contradictory-flags (Disallow flags or implications overriding each other.)
type: bool default: true
--allow-overwriting-for-next-flag (temporary disable flag contradiction to allow overwriting just the next flag)
type: bool default: false
--use-strict (enforce strict mode)
type: bool default: false
--harmony (enable all completed harmony features)
type: bool default: false
--harmony-shipping (enable all shipped harmony features)
@Drag13
Drag13 / v8-flags
Last active April 27, 2023 11:34
v8 engine flags for 9.4.143
Options:
--abort-on-contradictory-flags (Disallow flags or implications overriding each other.)
type: bool default: true
--allow-overwriting-for-next-flag (temporary disable flag contradiction to allow overwriting just the next flag)
type: bool default: false
--use-strict (enforce strict mode)
type: bool default: false
--harmony (enable all completed harmony features)
type: bool default: false
--harmony-shipping (enable all shipped harmony features)
function spyFactory(array, prepushHandler, postpushHandler) {
const prepush = functionify(prepushHandler);
const postpush = functionify(postpushHandler);
const interceptor = {
get: function (obj, prop) {
if (prop !== 'push') { return obj[prop]; }
return function (...args) {
prepush(args);
Array.prototype.push.apply(this, args);
function spyFactory(array) {
const interceptor = {
get: function (obj, prop) {
if (prop !== 'push') { return obj[prop]; }
return function(...args) {
console.log('prepush');
Array.prototype.push.apply(this, args);
console.log('postpush');
}
function MyArray(arr) {
const _arr = arr || [];
this.push = function push(value) {
console.log('prepush');
_arr.push(value)
console.log('postpush');
}
this.get = function getByIndex(index) {
const originalPush = Array.prototype.push;
Array.prototype.push = function newPush() {
console.log('before push');
originalPush.call(this, arguments[0]);
console.log('after push');
}
let x = [];
x.push('a');
@Drag13
Drag13 / typescript.import.ts
Last active September 3, 2018 15:02
Importing json in typescript
// Fix that gives ability to import json files without TypeScript errors
declare module '*.json' {
const value: any;
export default value;
}
npm version patch -m "Version %s - added more documentationc"