> 
blog_rick_love>  

Emoji Variable Names

titleEmoji Variable Namesdate2020-10-30path/emoji-variable-namesauthorRick LoveexcerptEmoji Support for Variable Names in Typescriptimageemoji-variable-names.pngtagstypescript, evil-code

tl;dr: A quick update on the state of support for using emojis in the worst places: variable names

Summary

It looks like the options are limited to within string contexts. Furtunately for those who want the most unreadable code ever, it is possible to at least name all your object fields with emojis.

/** ❌ variable declarations */
// const 😾 = false;
// const _😥 = false;
// const [`_😥`] = false;

/** ❌ function declarations */
// export function _😾  () { };

/** ❌ function parameters */
// export function o_o (😾: boolean) { };

/** ✔ function parameter literal string type */
export function o_o(cat: '😺') { };

/** ✔ Object Fields with String Names */
const profile = {
    [`🏷️`]: `Rick Love`,
    [`🤓`]: true,
    ['🧙‍♂️']: true,
};

/** ✔ Types support - string literals */
type EmojiType = '🤓' | '🧙‍♂️';

/** ✔ Emoji Enum */
enum EmojiEnum {
    [`🏷️`] = `🏷️`,
    [`🤓`] = `🤓`,
    ['🧙‍♂️'] = '🧙‍♂️',
}
const emojiEnum = EmojiEnum[`🏷️`];

/** ❌ export */
// export { profile[`🏷️`] as 🏷️ };

/** ❌ catch error parameter name */
// const tryCatchBlock = () => {
//     try {
//         const ok = true;
//     } catch (🥵) {
//     }
// };

Most Destruction Possible

If you really want to have fun, make sure to use the zero-width-space in combination with string emoji fields:

/** ### Have Fun! 
 * 
 * - Note: these are not the same
 * - Hint: zero-width-space
*/
const criticalSwitch = (value: '😇' | '​😇' | '😇​' | '​') => {
    switch (value) {
        case '😇': return '😳';
        case '​😇': return '​😳';
        case '😇​': return '😳​';
        case '​': return '​';
        default: return '';
    }
};

Conclusion

You're welcome!

Related Articles

2020-11-03Course: Todo App (Unfinished)typescript2020-10-30Emoji Variable Namestypescript, evil-code2020-10-24Typescript Type System Adventuretypescript2020-10-13Typescript 'any' is eviltypescript

Comments

Root

© 2020 Rick Love