{"id":19093173,"url":"https://github.com/ufocoder/wtfts","last_synced_at":"2026-03-04T10:02:15.710Z","repository":{"id":92639788,"uuid":"358551118","full_name":"ufocoder/wtfts","owner":"ufocoder","description":"Tricky TypeScript Examples","archived":false,"fork":false,"pushed_at":"2021-04-24T19:25:57.000Z","size":35,"stargazers_count":11,"open_issues_count":2,"forks_count":0,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-02-22T07:51:16.569Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"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/ufocoder.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-04-16T09:47:23.000Z","updated_at":"2024-11-17T16:06:28.000Z","dependencies_parsed_at":"2023-03-09T07:30:11.490Z","dependency_job_id":null,"html_url":"https://github.com/ufocoder/wtfts","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ufocoder/wtfts","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ufocoder%2Fwtfts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ufocoder%2Fwtfts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ufocoder%2Fwtfts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ufocoder%2Fwtfts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ufocoder","download_url":"https://codeload.github.com/ufocoder/wtfts/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ufocoder%2Fwtfts/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30078307,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-04T08:01:56.766Z","status":"ssl_error","status_checked_at":"2026-03-04T08:00:42.919Z","response_time":59,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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-09T03:23:32.551Z","updated_at":"2026-03-04T10:02:15.694Z","avatar_url":"https://github.com/ufocoder.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# wtfts\nTricky and non obvious TypeScript Examples\n\n👋🏻 Welcome to add new trick examples or to improve old one\n\n# Table of Contents\n\n* [Enum option](#enum-option)\n* [Invariant](#invariant)\n* [Comparing two functions](#comparing-two-functions)\n* [Interface redeclaration](#interface-redeclaration)\n* [Interfaces with similar structure](#interfaces-with-similar-structure)\n\n## Enum option\n\nTypeScript code:\n\n```typescript\nconst optionA = 'variableValueA';\n\nenum CustomEnum {\n    optionA = 'enumValueA',\n    optionB = optionA\n}\n\n// CustomEnum.optionB !== optionA\n```\n\n[Playground](https://www.typescriptlang.org/play?ts=4.2.3\u0026ssl=1\u0026ssc=1\u0026pln=6\u0026pc=2#code/MYewdgzgLgBCAOUCW4CCMC8MDkA3AhgE5L4BGANgKYBq+5ArpatgNwCwAUJ5WPQLYwAwvWgg+AUV4CA3pxjy4iFGHRZsPfrQZNsAGjkKEycACFMi4ys4BfIA)\n\n**Explanation**: \n\nValue for `optionB` use `optionA` from enum score.\n\nWill be compiled to:\n\n```javascript\n\"use strict\";\nconst optionA = 'variableValueA';\nvar CustomEnum;\n(function (CustomEnum) {\n    CustomEnum[\"optionA\"] = \"enumValueA\";\n    CustomEnum[\"optionB\"] = \"enumValueA\";\n})(CustomEnum || (CustomEnum = {}));\n```\n\n## Invariant\n\nThe following code will be compiled without errors:\n\n```typescript\ninterface Animal { name: string }\ninterface Cat extends Animal { meow: () =\u003e string }\ninterface Dog extends Animal { wow: () =\u003e string }\n\nconst addAnimal = (animals: Animal[]) =\u003e {\n    const cat: Cat = { name: 'Cat #1', meow: () =\u003e 'meow' };\n    animals.push(cat);\n}\n\nconst dogs: Dog[] = [];\n\naddAnimal(dogs);\n\ndogs[0].wow()\n```\n\n[Playground](https://www.typescriptlang.org/play?ts=4.2.3#code/JYOwLgpgTgZghgYwgAgIImAWzgG2Qb2RDkwgC5kBnMKUAc2QF8BYAKFElkRQGE4xkEAB6QQAE0poM2PIVIB7AO4UAFAEpkAXgB8VGvSZsO0eEmQAReQ2GiJUrLgLJFS1Rp17aIBi1ZsE8iDUyHBiYugOeJrIKnDSuJQUETIA2gC67rr4bMi5yAFBAgj8FHwC0YTEpBQA5GXIAMQAjDUANMgKyjGZyDWdNUwA3Dl5cZGUAHQADgCulAAWKsVgasOsvv6BwWJWiRZW6VrI6WtsoeHxOCo7dJSrbGw3lCkADGkTLorqbEA)\n\n\n## Comparing two functions\n\n```typescript\nlet x = (a: number) =\u003e a;\nlet y = (b: number, s: string) =\u003e s ? b : 0;\n\ny = x; // OK\nx = y; // Error\n```\n\n[Playground](https://www.typescriptlang.org/play?ssl=1\u0026ssc=1\u0026pln=5\u0026pc=16#code/DYUwLgBAHhC8EAoCGAuCA7ArgWwEYgCcBKOAPgiQG4BYAKFEgE85Fc0s9CAaCAZzV5gCAS3QBzErHK8IAfgi4IaAAw1adZvCiUIAel0QA8gGs6MeIx36IAUQIEA9gSA)\n\nExplanation from [official documentation](https://www.typescriptlang.org/docs/handbook/type-compatibility.html#comparing-two-functions)\n\n## Interface redeclaration\n\nThe following code:\n\n```typescript\ninterface Box {\n    size: number;\n    color: string;\n}\n\n// .. many lines of code here\n\ninterface Box {\n    weight: number;\n}\n\nconst box: Box = {\n    weight: 5\n}\n```\n\nhas compile error:\n\n```\nType '{ weight: number; }' is missing the following properties from type 'Box': size, color\n```\n\n[Playground](https://www.typescriptlang.org/play?ts=4.2.3#code/JYOwLgpgTgZghgYwgAgEIHsAeyDeBYAKGWOQGdgAvCALmRAFcBbAI2gG5CTkF0AbdKLVJgooAOYcCAX0KEA9HOQA6JckZwQAT2S9QEUsnQxu6ACYoAFtAiyCoSLEQoM2fERIB3CMDEWwtBhZ2QhkCQh4QYWRmLFoXZABeXE5Pb19-ZABWENsgA)\n\n**Explanation**: Interfaces not are overridden by new declations. \n\n## Interfaces with similar structure\n\n```typescript\ninterface Entity {\n  name: string;\n}\n\n\ninterface Enemy {\n  name: string;\n  health: number;\n  attack: number;\n}\n\ninterface Player {\n  name: string;\n  health: number;\n  attack: number;\n}\n\nlet entity: Entity = { name: 'Mario' }\nlet enemy: Enemy = { name: 'Shredder', health: 100, attack: 20 };\nlet player: Player = { name: 'John Cena', health: 110, attack: 35 };\n\nenemy = player; // OK ?!\nplayer = enemy; // OK ?!\n\nentity = enemy; // OK ?!\nenemy = entity; // Error\n```\n\n[Playground](\nhttps://www.typescriptlang.org/play?ssl=1\u0026ssc=1\u0026pln=27\u0026pc=1#code/JYOwLgpgTgZghgYwgAgKLmGAnsg3gWAChlkQ4BbCALmQGcwpQBzAbiIF8ivDRJZEU6CORwFipCtToNmbcQAsIcADZh5NEAFdyAI2hyScMGEQBrDdr1Q5nQkV7R4SZAAVlcLNDxESZSjXpGEFYfZEUVNQtdfVCjEwRzUksYwlsiZQgwZAgMbBp0MEwcAF48CX9kAHIAWThGAHtK5FsMrJzhLHyQDuRS3HKpSoBleSgIABNx6EqAGjClVXVkAEYABlW5uLMaACZV5rlW5AAHd08oGjcPLz6BmkqAKXr5EGQAYRy4WfmIpeW1zbGbbIADMAFYDtx2iJeiczvpkAB6RHIADyAGlkAB+ACERFO1ygsOhWBYSJRGOxeLshByhWwxO6IjJyLRmNxRBJjPppPJaCgUHqUCIQA)\n\nExplanation: TypeScript is based on structural subtyping\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fufocoder%2Fwtfts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fufocoder%2Fwtfts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fufocoder%2Fwtfts/lists"}