Skip to content

Object non-literal keys breaks parameter type guard in callback value #29718

Open
@MasGaNo

Description

@MasGaNo

TypeScript Version: 3.4.0-dev.20190202

Search Terms:
object dynamic keys callback implicit any
duplicate dynamic object key typing
Code

// PLEASE ENABLE => noImplicitAny 

type Keys = 'a' | 'b' | 'c';
type Shape = { [K in Keys]?: K extends string ? (key: K) => K : never };

const getC = () => 'c' as 'c';

export const obj: Shape = {
  ['a']: key => key, // OK => inferred correctly
  ['b' as 'b']: key => key, // Error => inference broken "any" type
  [getC()]: key => key, // Error => inference broken "any" type
  [getC()]: key => key, // Error => inference broken "any" type instead of Duplicate identifier
  ['a']: key => key, // OK => Duplicate identifier as expected
};

Expected behavior:

  • No implicit any error, but correct type assignation
  • Detect duplicate identifier in case of non-explicit literal string key

Actual behavior:

  • When not using an explicit literal string as an object-key, TypeScript cannot infer correctly the value.
  • In a same way, TypeScript cannot detect duplicate identifier

Playground Link:
(http://www.typescriptlang.org/play/#src=%2F%2F%20PLEASE%20ENABLE%20%3D%3E%20noImplicitAny%20%0D%0A%0D%0Atype%20Keys%20%3D%20'a'%20%7C%20'b'%20%7C%20'c'%3B%0D%0Atype%20Shape%20%3D%20%7B%20%5BK%20in%20Keys%5D%3F%3A%20K%20extends%20string%20%3F%20(key%3A%20K)%20%3D%3E%20K%20%3A%20never%20%7D%3B%0D%0A%0D%0Aconst%20getC%20%3D%20()%20%3D%3E%20'c'%20as%20'c'%3B%0D%0A%0D%0Aexport%20const%20obj%3A%20Shape%20%3D%20%7B%0D%0A%20%20%5B'a'%5D%3A%20key%20%3D%3E%20key%2C%20%2F%2F%20OK%20%3D%3E%20inferred%20correctly%0D%0A%20%20%5B'b'%20as%20'b'%5D%3A%20key%20%3D%3E%20key%2C%20%2F%2F%20Error%20%3D%3E%20inference%20broken%20%22any%22%20type%0D%0A%20%20%5BgetC()%5D%3A%20key%20%3D%3E%20key%2C%20%2F%2F%20Error%20%3D%3E%20inference%20broken%20%22any%22%20type%0D%0A%20%20%5BgetC()%5D%3A%20key%20%3D%3E%20key%2C%20%2F%2F%20Error%20%3D%3E%20inference%20broken%20%22any%22%20type%20instead%20of%20Duplicate%20identifier%0D%0A%20%20%5B'a'%5D%3A%20key%20%3D%3E%20key%2C%20%2F%2F%20OK%20%3D%3E%20Duplicate%20identifier%20as%20expected%0D%0A%7D%3B%0D%0A)

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions