Skip to content

Cannot find name and using private name when export a type. #22220

Closed

Description

typescript: 2.7.2
vscode: 1.20.1
tsconfig:

{
  "compilerOptions": {
    "target": "es5", 
    "module": "es2015", 
    "lib": ["es2015", "es2015.core", "dom"],
    "jsx": "react", 
    "declaration": true, 
    "removeComments": true,  
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "moduleResolution": "node", 
    "typeRoots": ["./typings", "./node_module/@types"],
    "types": ["modules", "node", "dom-align", "css-animation"],
    "allowSyntheticDefaultImports": true
  }
}
export type OneOf<T extends any[]> = T[number];

export const placements: string[] = [
  'top',
  'left',
  'right',
  'bottom',
  'topLeft',
  'topRight',
  'bottomLeft',
  'bottomRight',
  'leftTop',
  'leftBottom',
  'rightTop',
  'rightBottom',
];

export type Placement = OneOf<placements>;

lark20180228150426

Expected behavior: It should work well.
Actual behavior: Throw errors that cannot find name and using private name.

But if I replace the generic placements to the primitive array, it would works.

export type OneOf<T extends any[]> = T[number];

export const placements: string[] = [
  'top',
  'left',
  'right',
  'bottom',
  'topLeft',
  'topRight',
  'bottomLeft',
  'bottomRight',
  'leftTop',
  'leftBottom',
  'rightTop',
  'rightBottom',
];

export type Placement = OneOf<[
  'top',
  'left',
  'right',
  'bottom',
  'topLeft',
  'topRight',
  'bottomLeft',
  'bottomRight',
  'leftTop',
  'leftBottom',
  'rightTop',
  'rightBottom'
]>;

And this code would not cause that errors.

Activity

  1. vincentdchan commented on Feb 28, 2018

    @vincentdchan

    The variable placements will not be regarded as a type. It's a value. That's why you can not use generic type such as OneOf.

  2. vincentdchan commented on Feb 28, 2018

    @vincentdchan

    In this situation, just use:

    type Placeholder = 
    | 'top',
    | 'left',
    | 'right',
    | 'bottom',
    | 'topLeft',
    | 'topRight',
    | 'bottomLeft',
    | 'bottomRight',
    | 'leftTop',
    | 'leftBottom',
    | 'rightTop',
    | 'rightBottom'
    
  3. sheltondong commented on Feb 28, 2018

    @sheltondong
    Author

    But it is still a value when I using primitive array, why should that works?

    export type Placement = OneOf<[
      'top',
      'left',
      // ...
    ]>;

    This is still a value

  4. ghost added
    QuestionAn issue which isn't directly actionable in code
    on Feb 28, 2018
  5. typescript-bot commented on Mar 14, 2018

    @typescript-bot
    Contributor

    Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

  6. locked and limited conversation to collaborators on Jul 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions