Skip to content
tinytip

Latest tips / 13

Reuse the last git commit message in VS Code

June 27, 2021
#vscode

When you add a new git commit in VS Code you can press Arrow Up in the commit message input to get the previous commit message. Press it several times to get older commit messages.

Demo of how to get the previous commit message in VS Code

Note: this only works for changes committed with the source control panel in VS Code. It will not show commits from external tools like SourceTree or the terminal.

TypeScript supports arrays with the rest syntax at the beginning

June 20, 2021
#typescript

A rest parameter in a JavaScript function must be the last parameter of the list:

// Does not work
function example(...arg, value) {}

// Works
function example(value, ...args) {}

However, you can use the rest syntax for an array type in TypeScript at any position.

// Expect a list of strings and one number as last parameter
function example(...args: [...string[], number]) {}

// It can also be in the middle of an array
type Example = [boolean, ...string[], number];

Define an array with a min length in TypeScript

June 20, 2021
#typescript

You can create an array type with a min length in TypeScript by using the rest syntax.

// Type requires at least one element
type NonEmptyArray<T> = [T, ...T[]];

// Type requires at least three elements
type ThreeOreMoreArray = [T, T, T, ...T[]];

You can now use the type and TypeScript ensures that the array meets the minimum length:

function example(items: NonEmptyArray<string>) {}

// Error: Source has 0 element(s) but target requires 1.
example([]);

// Works
example(["one"]);
example(["one", "two", "three"]);

Style console log outputs in JavaScript

June 20, 2021
#javascript

Console Log statements can be styled with a limited set of CSS features. Insert %c in your log message add an additional parameter with the style.

// Logs "hello world" in red letters
console.log("%chello world", "color:red");

// Use multiple "%c" and multiple style arguments
// Logs "hello world" and "hello" is red
console.log("%chello %cworld", "color:red", "");

// "hello" is italic and "world" has a red border and red text color
console.log(
"%chello %cworld",
"font-style:italic" /* styles for "hello" */,
"border:1px solid red;color:red" /* styles for "world" */
);

Here is how it looks like on your browser console:

Styled color logs in the browser console

Amend the previous commit and keep the commit message

June 20, 2021
#git

Did you just commit some changes and forget to delete that console.log('test') statement? Use git commit with --amend to edit the previous commit and --no-edit to keep the previous commit message.

# 1. Delete the console.log

# 2. Stage the changes
git add my-file.js

# 3. Commit by updating the previous commit
git commit --amend --no-edit

Do this for local commits only. Don't amend commits that you already pushed.