Skip to main content
Mark functions, classes, or fields as deprecated, reminding developers to use alternative solutions.

Syntax

---@deprecated [alternative_explanation]

Purpose

Deprecation markers:
  • Warn users about outdated APIs
  • Guide developers to modern alternatives
  • Enable gradual migration from old to new code
  • Generate documentation with deprecation notices

Examples

Simple Deprecation

---@deprecated
function oldFunction()
    print("This is an old function")
end

Deprecation with Alternative

---@deprecated Please use newCalculateSum function instead
---@param numbers number[] Array of numbers
---@return number Sum
function calculateSum(numbers)
    local sum = 0
    for _, num in ipairs(numbers) do
        sum = sum + num
    end
    return sum
end

-- New replacement function
---@param numbers number[] Array of numbers
---@return number Sum
function newCalculateSum(numbers)
    return table.reduce(numbers, function(acc, num) return acc + num end, 0)
end

Deprecated Class

---@deprecated Please use ModernUser class instead
---@class OldUser
---@field id number
---@field name string
local OldUser = {}

-- New replacement class
---@class ModernUser
---@field id number
---@field name string
---@field email string
---@field createdAt string
local ModernUser = {}

Deprecated Field

---@class APIResponse
---@field success boolean
---@field data any
---@field message string
---@deprecated Please use errorMessage field instead
---@field error string

Deprecated Method

---@class FileManager
local FileManager = {}

---@deprecated Use readFileSync or readFileAsync instead
---@param path string File path
---@return string File content
function FileManager:loadFile(path)
    local file = io.open(path, "r")
    if file then
        local content = file:read("*a")
        file:close()
        return content
    end
    return ""
end

---@param path string File path
---@return string File content
function FileManager:readFileSync(path)
    local file = io.open(path, "r")
    if not file then
        error("Could not open file: " .. path)
    end
    local content = file:read("*a")
    file:close()
    return content
end

Version Information

---@deprecated Since v2.0.0, use authenticateUser instead
---@param username string Username
---@param password string Password
---@return boolean Whether login succeeded
function login(username, password)
    -- Old authentication logic
    return username == "admin" and password == "secret"
end

Conditional Deprecation

---@deprecated Only use for legacy compatibility, migrate to new API
---@param data table Legacy data format
---@return table Converted data
function convertLegacyData(data)
    print("WARNING: Using deprecated convertLegacyData function")
    -- Conversion logic
    return {
        id = data.old_id,
        name = data.old_name,
        type = "legacy"
    }
end

Usage Warnings

When deprecated functions are called, the analyzer will show warnings:
local result1 = oldFunction()  
-- Warning: Function is deprecated

local result2 = calculateSum({1, 2, 3})  
-- Warning: Please use newCalculateSum function instead

local user = OldUser.new()  
-- Warning: Please use ModernUser class instead

Migration Patterns

Gradual Migration

---@deprecated Use processDataV2 for better performance
function processData(data)
    -- Old implementation
    return processDataV2(data)  -- Forward to new version
end

function processDataV2(data)
    -- New, optimized implementation
    return optimizedProcess(data)
end

With Fallback Support

---@deprecated Will be removed in v3.0.0
function oldAPI()
    if ENABLE_LEGACY_SUPPORT then
        return legacyImplementation()
    else
        error("oldAPI has been removed. Use newAPI instead.")
    end
end

Best Practices

  1. Always provide alternatives: Tell users what to use instead
  2. Include version information: Specify when the feature was deprecated
  3. Plan removal: Indicate when the deprecated feature will be removed
  4. Keep deprecated code working: Don’t break existing users immediately
  5. Update documentation: Ensure docs reflect the deprecation
Deprecated APIs should still function correctly to avoid breaking existing code. Plan for eventual removal in a future major version.
Include clear migration guidance in your deprecation messages to help users transition smoothly to new APIs.

Features

Clear Warnings

IDE warnings for deprecated usage

Migration Guidance

Direct users to modern alternatives

Version Tracking

Track when features were deprecated

Gradual Migration

Support smooth transitions to new APIs

Build docs developers (and LLMs) love