Last active
September 20, 2024 21:16
Revisions
-
develax revised this gist
Sep 20, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -67,4 +67,4 @@ calling the function will cause no errors: stringifyPerson(alex3); /* No TypeScript errors */ ``` [PLAYGROUND](https://www.typescriptlang.org/play/?#code/PTAEEFQMwVwOwMYBcCWB7OokAsCGTRcEEBTAByQGdDNcAnAcxgFsS4C0oaaMBPZtDGpJeZEqADu2EnXHY0AIwUoS1FNVxYYZADYkAXAChDsRKgyhKSOijgMUUXgAUZlDAAoxdN3H2gA3oagwaBwuKx+VjZ2ANxBIbgMBqEsCjJxIaDySiqUfgDaUbYMADSW1sUAujGgIFq6JIYAvgCUAfHBskgwdJgABgAk-l4+AHRhrE2g6qBDIxijiSRTvCT01Gg6ACY0OzpoAG6qs8OuC9nKqqMAVmi27gBEu8EPLU2jfXFNxnUIsvjiTSUFDMFA6ehYUQGYwIDBWQh6AAeoAAvO1MhNkgBycBIrElDqEJJ+ABMAAYCZkLrkCliJLgdABrYr40BY2FoZl2LGVWpgaSyNnU1R9aYaOh0XC8MpwNAEbpkBqRCp2fK8gCEaMKKtK5WiDEqzR+YGsvGKWDQoAQDJ0WGk0HgyHQmAkKBwCJIyNw4qYrHYkjBtutQkBmBkdDQdCMhiKdgczjOcHcDM9LRqdQAKlC9cU1WLQnLCJRgQwwgo9BbIWJQNr9WVYwbjaAADIkJBY6hbEgIcGCkTVm0ob0B90KvTR-viFzeCxowIY8LJBsZBLElLMNJ0FfBYV5GsN+s66p8+p6I2GOq4OA7P5rJChuUCwiMFhsDhcHAzSegT26FAIN0dF4aNYTgeEU0REk-GnHxUXREJMT8HE8UpVdknJVCd0US493yOkGS5BhWXZNBORZXk6llCRpgIL9tArNE+lrYpD31So+nPOocDYK0bTtcRTCdDBowbeMYI8CCSTTE8qJojtQAAeUZJsAHU8ForheEEUAtgwLECHpf0kEtW8AR-RE-wA+UoQAflAABJMD71wHYtJgUBmClSQr3lS1bCgGR+KrASI2YILGSozBBPMWgXz9JBRlAAAxSM7XwUA3N4zBTPvQhQCSOAZH-YK0oIa1MH8wKcHS78oFStzRmMb8ksdJKUG8JBwDit8szEAAeDMAD44Izcz72vah3CgdqrD8Sq6FADM2o6spRjW+gGD3K9eDVNoUWG7b4jspaZqQeI-EKo4tybK8diSeV1OCids3EuBHICiUSC2XrxDRFrEGWqwut9HqoT6ydOBzONHFewa4gvMAM3tV73vDL6fvzFAu3Yf8GUrX8dH-QDeB0kgdGtWQdlekrbqy0A0lAEMdm4yxF284CYThAgIIAZmgxNUc+77sznQlELZXFPXxQkllJClCV3Wl6SZFkyhIsjuQosA3XzMdftAJiDyhg0OO+BHeJ0Qm7CC6LnQDS3eJDAsfwlSM8mMUSYcTZMkR56TgAAKlAAA5S0foAZT+FAKBdiNvFAAOwCAA) -
develax revised this gist
Sep 20, 2024 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -66,3 +66,5 @@ calling the function will cause no errors: ```TS stringifyPerson(alex3); /* No TypeScript errors */ ``` [PLAYGROUND](https://www.typescriptlang.org/play/?#code/PTAEEFQMwVwOwMYBcCWB7OokAsCGTRcEEBTAByQGdDNcAnAcxgFsS4C0oaaMBPZtDGpJeZEqADu2EnXHY0AIwUoS1FNVxYYZADYkAXAChDsRKgyhKSOijgMUUXgAUZlDAAoxdN3H2gA3oagwaBwuKx+VjZ2ANxBIbgMBqEsCjJxIaDySiqUfgDaUbYMADSW1sUAujGgIFq6JIYAvgCUAfHBskgwdJgABgAk-l4+AHRhrE2g6qBDIxijiSRTvCT01Gg6ACY0OzpoAG6qs8OuC9nKqqMAVmi27gBE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 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # Infer Function Argument Type A function that accepts an argument of an anonymous type where `hobbies` is a tuple: ```TS -
develax revised this gist
Sep 20, 2024 . 1 changed file with 0 additions and 9 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,14 +1,5 @@ # Infer Types A function that accepts an argument of an anonymous type where `hobbies` is a tuple: ```TS function stringifyPerson(person: { -
develax revised this gist
Sep 20, 2024 . 1 changed file with 25 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -50,4 +50,28 @@ const alex2: Person = { then call the function: ```TS stringifyPerson(alex2); // now it's Ok ``` What if you don't want to create explicit type? Instead you may want to *infer* the type from the known function argument. For that you can create a generic type that can infer that type for you. ```TS type FuncFirstArgumentType<T> = T extends (first: infer TFirst, ...args: any[]) => any ? TFirst : never; ``` and get that type: ```TS type PersonInferredType = FuncFirstArgumentType<typeof stringifyPerson>; ``` The `PersonInferredType` is identical to explicitly delcared `Person` type and can be used the same way: ```TS const alex3: PersonInferredType = { name: 'Alex', age: 20, hobbies: ['walking', 'cooking'] // it is tuple = `[string, string]` } ``` calling the function will cause no errors: ```TS stringifyPerson(alex3); /* No TypeScript errors */ ``` -
develax created this gist
Sep 20, 2024 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,53 @@ # Infer Types We have a type: ```TS type Person = { name: string; age: number; hobbies: [string, string]; // tuple } ``` A function that accepts an argument of an anonymous type where `hobbies` is a tuple: ```TS function stringifyPerson(person: { name: string; age: number; hobbies: [string, string]; // tuple }) { return `${person.name} is ${person.age} years old and loves ${person.hobbies.join(" and ")}.`; } ``` create a similar type: ```TS const alex = { name: 'Alex', age: 20, hobbies: ['walking', 'cooking'] // here 'hobbies` is array, not tupple: string[] != [string, string] } ``` trying to call the function with `alex` as argument will cause an error: ```TS stringifyPerson(alex); // Type string[] is not assignable to type [string, string] ``` Let's declare type allias with tuple: ```TS type Person = { name: string; age: number; hobbies: [string, string]; // tuple } ``` and create another argument of this type explicitly: ```TS const alex2: Person = { name: 'Alex', age: 20, hobbies: ['walking', 'cooking'] // now it is tuple = `[string, string]` } ``` then call the function: ```TS stringifyPerson(alex2); // now it's Ok ```