Skip to content

Instantly share code, notes, and snippets.

@GerkinDev
Last active September 24, 2018 21:10
Show Gist options
  • Save GerkinDev/a755a685a1a11c72d10bab86e92a82f9 to your computer and use it in GitHub Desktop.
Save GerkinDev/a755a685a1a11c72d10bab86e92a82f9 to your computer and use it in GitHub Desktop.
Tsar spec

Slim arrow await operator (TS & JS)

The await operator can be used to chain async functions calls in a more elegant way. It is formed by a slim arrow (->), in place of the property accessor operator ..

Example :

const promiseResult = foo->bar();
const promiseResult = (await foo.bar());

The await operator is wrapped in parenthesis to allow chained call.

const promiseResult = foo->bar().baz()->qux();
const promiseResult = (await (await foo.bar()).baz().qux());

It can be used on any methods returning a promise, or any promise object property, but due to the javascript behavior of the await operator, any non-promise return value or property is valid too (but disencouraged, see tslint rule).

Because it is used in place of the property accessor operator ., it can't be used to await a promise variable.

Optionnal accessing operator (TS & JS)

const foo = bar?.qux;
const baz = quux?.boot++
const foo = (isNil(bar) ? bar.qux : bar);
const baz = (isNil(quux) ? quux.boot++ : quux);

Interface type guards (TS only)

This feature is composed of a marker on the interface to define the type guard(s) property(ies), and the keywords is & by new keywords.

interface Bar {
    foo: 'bar';
}
interface Baz {
    foo: 'baz';
}

const obj!: Bar | Baz;
if(obj is Bar by 'foo') {
    // obj is Bar
}
interface Bar {
    foo: 'bar';
}
interface Baz {
    foo: 'baz';
}

const obj!: Bar | Baz;
if(obj.foo === 'bar') {
    // obj is Bar
}

Preprocessor directives (TS & JS)

Preprocessor directives are normal language instruction (TS or JS) executed at build time, and marked by a leading #. Each non-preprocessor instruction ran by the preprocessor logic is appended to the output, with their output replaced.

# const FOO = true;
# if(FOO){
echo 'foo';
# } else {
echo 'bar';
# }

Output:

echo 'foo';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment