Created
April 25, 2025 07:54
-
-
Save TotooriaHyperion/7206eb1b4a7be28783e6773912648ba8 to your computer and use it in GitHub Desktop.
Good part of typescript namespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 [{}]; | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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