Fix string length type function example in Luau blog post

This commit is contained in:
Erica Marigold 2024-10-29 18:07:17 +05:30 committed by GitHub
parent 822656c330
commit cc1073517c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -60,21 +60,22 @@ localeFn("en-gb") // TypeError: Argument of type 'string' is not assignable to p
```
```luau
type function IsStringOfLength(s: string, length: number)
if #s ~= length then
type function IsStringOfLength(s, length)
local lenNum = tonumber(len:value())
assert(s:is("singleton") and typeof(s:value()) == "string", "s must be of type string")
assert(len:is("singleton") and lenNum ~= nil, "len must be of type number")
if #s:value() ~= lenNum then
return error("Expected country code to be 2 characters")
end
return s
end
type LocaleCountryCode<T> = IsStringOfLength<T, 2>
local function localeFn<T>(code: LocaleCountryCode<T>)
-- Do something
end
type LocaleCountryCode<T> = IsStringOfLength<T, "2">
localeFn("gb") -- This is fine
localeFn("en-gb") -- TypeError: Expected country code to be 2 characters
local gb: LocaleCountryCode<"gb"> -- This is fine
local enGb: LocaleCountryCode<"en-gb"> -- TypeError: Expected country code to be 2 characters
```
The Luau implementation in the form of a type function is not only more readable and intuitive, it also allows for entirely customizable error diagnostics. Such a pattern is common in Luau's type system design, which emphasizes on types not overshadowing code or requiring a pHD to write due to complexity.