{"id":18676254,"url":"https://github.com/cgarciae/typejs","last_synced_at":"2025-11-07T06:30:28.669Z","repository":{"id":74614591,"uuid":"20890346","full_name":"cgarciae/TypeJS","owner":"cgarciae","description":"Automatic Type-checking for JavaScript Functions!","archived":false,"fork":false,"pushed_at":"2014-06-16T20:17:53.000Z","size":216,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-27T20:43:46.158Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cgarciae.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-06-16T15:38:30.000Z","updated_at":"2024-03-22T17:46:57.000Z","dependencies_parsed_at":"2023-02-26T05:45:14.534Z","dependency_job_id":null,"html_url":"https://github.com/cgarciae/TypeJS","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cgarciae%2FTypeJS","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cgarciae%2FTypeJS/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cgarciae%2FTypeJS/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cgarciae%2FTypeJS/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cgarciae","download_url":"https://codeload.github.com/cgarciae/TypeJS/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239520675,"owners_count":19652735,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-07T09:28:27.161Z","updated_at":"2025-11-07T06:30:28.627Z","avatar_url":"https://github.com/cgarciae.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"TypeJS\n======\n\nAutomatic Type-checking for JavaScript Functions! TypeJS is intented to enhance functional programming JavaScript libraries by adding Haskell-style type definition to functions.\n\n## Basic Example\nLets construct a peculiar `Add` function\n```js\nvar Add = Type ('Num -\u003e Num -\u003e Int', function (a, b) {\n\treturn a + b;\n});\n```\n\n`Add` takes two `Num`s and returns an `Int`, since `Num` is any number in general but `Int` is more strict, this `Add` function will only be work if the two numbers given sum to a whole number.\n\n```js\nAdd(1, 2) //3\nAdd(1.3, 3.7) //5\nAdd(2, 2.2) //TypeError: Incorrect output type, expected Int, received Num\nAdd(2, '3') //TypeError: Incorrect input type, expected Num, received String\n```\n\n## Type Variables\nLets modify `Add`s type signature a little and add a variable type `a`. Type variables have to be A SINGLE LOWERCASE LETTER!!!\n```js\nvar Add = Type ('Num -\u003e a -\u003e a', function (a, b) {\n\treturn a + b;\n});\n```\nThe variable type will be infered on the first use of the function.\n```js\nAdd (1, '2') //\"12\"\nAdd (1, 2) //TypeError: Incorrect input type, expected String, received Int\n```\n\n`Int`s will always be infered as `Num`s because in JS there is no way to diferentiate `3.0` from `3`. \n\n## Lists\nList types are compound types and are defined as in Haskell like `[a]`. Variable types within lists are infered as you would expect.\n\n```js\nvar sumReduce = Type ('[a] -\u003e a', function (l) {\n\treturn l.reduce( function(a,b) {return a + b;} );\n});\n```\n`a` will first be infered from the list and will be expected as the output type.\n```js\nsumReduce (['1','2','3']) //\"123\"\nsumReduce ([1,2,3]) //TypeError: Incorrect input type, expected [String], received [Int]\n```\nAny number of list nests like `[[[a]]]` will work. However, the type definition for the list will be based on its first element, type consistency withing the list will never be supported. For example, `[1,'2']` is of type `[Int]` while `['2',1]` is of type `[String]`.\n\n## Coherence\nIf a function is incorrectly typed it won't work, even in the first use.\n```js\nvar tellSum = Type ('a -\u003e a -\u003e a', function (num1, num2) {\n\treturn \"The sum is \" + (num1 + num2);\n});\ntellSum (1,2) //TypeError: Incorrect output type, expected Int, received String\n```\n\n## setType\nYou can create a new typed function for any existing untyped function with the setType() method added to Function.prototype\n```js\ng = f.setType ('a -\u003e a -\u003e a');\n```\n## Currently Supported Types\n\n\u003ctable style=\"width:300px\"\u003e\n\u003ctr\u003e\n  \u003ctd\u003e2\u003c/td\u003e\n  \u003ctd\u003eInt\u003c/td\u003e \n\u003c/tr\u003e\n\u003ctr\u003e\n  \u003ctd\u003e2.0\u003c/td\u003e\n  \u003ctd\u003eInt\u003c/td\u003e \n\u003c/tr\u003e\n\u003ctr\u003e\n  \u003ctd\u003e2.1\u003c/td\u003e\n  \u003ctd\u003eNum\u003c/td\u003e \n\u003c/tr\u003e\n\u003ctr\u003e\n  \u003ctd\u003e\"2\"\u003c/td\u003e\n  \u003ctd\u003eString\u003c/td\u003e \n\u003c/tr\u003e\n\u003ctr\u003e\n  \u003ctd\u003e[2,3]\u003c/td\u003e\n  \u003ctd\u003e[Int]\u003c/td\u003e \n\u003c/tr\u003e\n\u003ctr\u003e\n  \u003ctd\u003e[[1,2],[3,4]]\u003c/td\u003e\n  \u003ctd\u003e[[Int]]\u003c/td\u003e \n\u003c/tr\u003e\n\u003c/table\u003e\n\n\n\n## Missing\n1. Add the rest of JavaScript types (easy)\n2. Add type() method to Function.prototype you can access a functions type (relatively easy)\n3. Support type definition for functions that take functions such as `(a -\u003e b) -\u003e a -\u003e b` (the previous point helps but remains not easy)\n4. Make the return function a curried function that checks the types of the arguments as you add them (currently you can curry the typed function, but types will only be checked when the functions is actually excecuted.\n\n## Warning\nTyping a function has the extra cost of checking the type of all the arguments and output, don't use this in performance critical applications.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcgarciae%2Ftypejs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcgarciae%2Ftypejs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcgarciae%2Ftypejs/lists"}