Skip to content

Missing data type in requiring json file. #18001

@zheeeng

Description

@zheeeng

TypeScript Version: 2.4.0 / nightly (2.5.0-dev.201xxxxx)

Code

// data.json
{
    "age": 123,
    "username": "Tom",
}
declare const require: any;

// 'age' and 'username' are inffered as any
const { age, username } = require('./data.json')

// wrong syntax
const { age as number, username as string } = require('./data.json')

// can't use destruction, and we cant get warnings when we assert incorrect types
const age: number = require('./data.json').age
const username: string = require('./data.json').username

Expected behavior:

Get the correct types of 'age' and 'username'

Actual behavior:

Thus require syntax can't get the actual types when handling json file.

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