Skip to content

Instantly share code, notes, and snippets.

@TotooriaHyperion
Created April 25, 2025 07:54
Show Gist options
  • Save TotooriaHyperion/7206eb1b4a7be28783e6773912648ba8 to your computer and use it in GitHub Desktop.
Save TotooriaHyperion/7206eb1b4a7be28783e6773912648ba8 to your computer and use it in GitHub Desktop.
Good part of typescript namespace
export interface Something {
rules: Something.Rule[];
}
export namespace Something {
/**
* Rule is a pretty common name
* use namespace so we can easily avoid name collision without named import like
* import { Rule as ARule } from ./Something;
* import { Rule as BRule } from ./Otherthing;
*/
export interface Rule {}
export const isValid = (v: Something): boolean => {
return Boolean(v.rules.length);
};
export const defaultRules = (): Rule[] => {
return [{}];
};
}
import { Something } from './Something';
class B implements Something {
rules: Something.Rule[] = Something.defaultRules();
}
const b = new B();
const isValid = Something.isValid(b);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment