mirror of
https://github.com/luau-lang/luau.git
synced 2025-05-04 10:33:46 +01:00
Strengthen contract
This commit is contained in:
parent
24185678fe
commit
cc95264b48
1 changed files with 22 additions and 0 deletions
|
@ -89,6 +89,28 @@ local function processClientEvent(pet: unknown)
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
A type predicate function must return a boolean expression:
|
||||||
|
|
||||||
|
```lua
|
||||||
|
local function isPet(x: unknown): x :: Pet
|
||||||
|
return 5 -- TypeError: Type 'number' could not be converted into 'boolean'
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
If the variable specified is not a parameter to the function, we will emit a type error:
|
||||||
|
|
||||||
|
```lua
|
||||||
|
local function isPet(pet: unknown): x :: Pet -- TypeError: Unknown parameter 'x'
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
If the type we want to assert does not exist, we will emit a type error:
|
||||||
|
|
||||||
|
```lua
|
||||||
|
local function isDog(dog: unknown): dog :: Dog -- TypeError: Unknown type 'Dog'
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
## Drawbacks
|
## Drawbacks
|
||||||
|
|
||||||
One problem with user-defined type predicates, is that they do not completely solve the unsoundness issue, if the user
|
One problem with user-defined type predicates, is that they do not completely solve the unsoundness issue, if the user
|
||||||
|
|
Loading…
Add table
Reference in a new issue