Skip to content

Functions with intersection arguments are not always typed coherently #9514

Closed
@Elephant-Vessel

Description

@Elephant-Vessel

TypeScript Version: 1.8.10

Code

// tl;dr: 
// This should give a static error, but does not.
var monkeyAnalyzer: (input: Monkey) => void = (input: Monkey & Robot) => { console.log(input.FurColor, input.MotorCount); }
// Will definitely fail, as we can expect from the incompatible signatures of the declared type of monkeyAnalyzer and what we actually assign to it
monkeyAnalyzer(new Monkey()); 

// Verbose example
class Monkey
{
    public FurColor: string;
}

class Robot
{
    public MotorCount: number;
}

class MonkeyRobotAnalyzer
{
    public static Analyze = (input: Monkey & Robot): void =>
    {
        console.log(input.FurColor, input.MotorCount);
    }
}

class MonkeyManager
{
    constructor(monkeyAnalyzer: (input: Monkey) => void)
    {
        monkeyAnalyzer(new Monkey());
    }
}

MonkeyRobotAnalyzer.Analyze(new Monkey()); // Shows error as it should

var myManager = new MonkeyManager(MonkeyRobotAnalyzer.Analyze); // No error, but should not be allowed, signature of MonkeyRobotAnalyzer.Analyze is not compatible with (input: Monkey) => void

Expected behavior:

The incompatible signatures should result in a static error

Actual behavior:
We get no static error

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixedFixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions