Skip to content

Type incorrectly inferred as "string" rather than String Literal #22038

@benny-medflyt

Description

@benny-medflyt

Tested with:
TypeScript 2.6.2
TypeScript 2.7.2

Attached is an example where a string literal ("lastName") is being inferred as "string" rather than as the string literal "lastName". Adding a type assertion ("lastName" as any) causes compilation to succeed.

typescript_bug

class Q<s> {
    protected dummy: [Q<s>, s];
}

type MakeCols<s, T extends object> = {
    [P in keyof T]: Col<s, T[P]>;
};

class Col<s, a> {
    protected dummy: [Col<s, a>, s, a];
}

interface Link<Y extends object> {
    c: keyof Y;
}

interface ColDef<a extends object> {
    displayName: string | null;
    link: Link<a> | null;
}

type TableViewCells<T extends object> = {
    [P in keyof T]: ColDef<T>;
};

function declareTableViewThunk<a extends object>(query: <s>(q: Q<s>) => MakeCols<s, a>, colCells: () => TableViewCells<a>): void {
    query;
    colCells;
    throw new Error();
}

function personQuery<s>(q: Q<s>): {
    personId: Col<s, number>;
    lastName: Col<s, string | null>;
    firstName: Col<s, string>;
} {
    q;
    throw new Error();
}

export const agencyTableView = declareTableViewThunk(
    personQuery,
    () => ({
        personId: {
            displayName: "ID",
            link: {
                c: "lastName"
            }
        },
        lastName: {
            displayName: "Last Name",
            link: null
        },
        firstName: {
            displayName: "First Name",
            link: null
        }
    })
);
test.ts(43,5): error TS2345: Argument of type '() => { personId: { displayName: string; link: { c: string; }; }; lastName: { displayName: string...' is not assignable to parameter of type '() => TableViewCells<{ personId: number; lastName: string | null; firstName: string; }>'.
  Type '{ personId: { displayName: string; link: { c: string; }; }; lastName: { displayName: string; link...' is not assignable to type 'TableViewCells<{ personId: number; lastName: string | null; firstName: string; }>'.
    Types of property 'personId' are incompatible.
      Type '{ displayName: string; link: { c: string; }; }' is not assignable to type 'ColDef<{ personId: number; lastName: string | null; firstName: string; }>'.
        Types of property 'link' are incompatible.
          Type '{ c: string; }' is not assignable to type 'Link<{ personId: number; lastName: string | null; firstName: string; }> | null'.
            Type '{ c: string; }' is not assignable to type 'Link<{ personId: number; lastName: string | null; firstName: string; }>'.
              Types of property 'c' are incompatible.
                Type 'string' is not assignable to type '"lastName" | "personId" | "firstName"'.
{
    "compilerOptions": {
        "outDir": "../_build",
        "sourceMap": true,
        "inlineSources": true,
        "declaration": false,
        "removeComments": false,
        "newLine": "LF",
        "preserveConstEnums": false,
        "allowUnreachableCode": false,
        "allowUnusedLabels": false,
        "forceConsistentCasingInFileNames": true,
        "noFallthroughCasesInSwitch": true,
        "noImplicitAny": true,
        "noImplicitReturns": true,
        "noImplicitThis": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "strictNullChecks": true,
        "suppressExcessPropertyErrors": false,
        "suppressImplicitAnyIndexErrors": false,
        "lib": [
            "dom",
            "es2015.collection",
            "es2015.promise",
            "es2015.symbol",
            "es5",
            "es6"
        ],
        "noLib": false,
        "target": "es2015",
        "module": "commonjs",
        "moduleResolution": "node",
        "isolatedModules": false
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions