Static typing and get_viewport().size

Godot Version

v4.6.2.stable.official [71f334935]

Question

Trying to get better about static typing in GDscript. This code:

var screen_size: Vector2

func _ready() -> void:
	screen_size = get_viewport().size

Gives this warning:

W 0:00:01:354   GDScript::reload: The property "size" is not present on the inferred type "Viewport" (but may be present on a subtype).
  <GDScript Error>UNSAFE_PROPERTY_ACCESS

What’s the proper way to statically type get_viewport().size?

Well, as the error says - Viewport doesn’t have the size property. So you’d need to cast it first to it’s inherited type that has this property, like Window.

Or use get_window() function directly, if your node has this function.