Skip to content

Instantly share code, notes, and snippets.

@will-wow
Last active August 9, 2018 00:55
Show Gist options
  • Save will-wow/16c1fd80f759c9bb62a4051a0a3d6c0e to your computer and use it in GitHub Desktop.
Save will-wow/16c1fd80f759c9bb62a4051a0a3d6c0e to your computer and use it in GitHub Desktop.
Using Elixir-Style Modules in JavaScript
import React from ‘react’;
import {userType, fullName} from ‘./user’;
const UserComponent = user => (
<div>Name: {fullName(user)}</div>
);
UserComponent.propTypes = {
user: userType
};
import React from ‘react’;
import * as User from ‘./user’;
const UserComponent = user => (
<div>Name: {User.fullName(user)}</div>
);
UserComponent.propTypes = {
user: User.t
};
import * as User from './user';
class UserComponent {
name(): User.t {
return User.fullName(this.user);
}
}
import { userToFriend } from 'accounts/User';
userToFriend(user):
export const userType = PropTypes.shape({
firstName: PropTypes.string;
lastName: PropTypes.string;
email: PropTypes.string;
});
export function fullName(user) {
return `${user.firstName} ${user.lastName}`;
}
import * as User from 'accounts/User';
User.toFriend(user):
defmodule User do
defstruct [:first_name, :last_name, :email]
def full_name(user = %User{}) do
"#{user.first_name} #{user.last_name}
end
end
/**
* @typedef {Object} User
* @property {string} firstName
* @property {string} lastName
* @property {string} email
*/
/**
* @param {User} user
* @returns {string}
*/
export function fullName(user) {
return `${user.firstName} ${user.lastName}`;
}
export interface User {
firstName: string;
lastName: string;
email: string;
}
export function fullName(user: User): string {
return `${user.firstName} ${user.lastName}`;
}
export const t = PropTypes.shape({
firstName: PropTypes.string;
lastName: PropTypes.string;
email: PropTypes.string;
});
export function fullName(user) {
return `${user.firstName} ${user.lastName}`;
}
export interface t {
firstName: string;
lastName: string;
email: string;
}
export function fullName(user: t): string {
return `${user.firstName} ${user.lastName}`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment