Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Types are inferred too narrow from template strings in a readonly object starting from TypeScript 4.7 #49762

Closed
graphemecluster opened this issue Jul 2, 2022 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@graphemecluster
Copy link
Contributor

Bug Report

Credit: Translate from this tweet by @MH4GF

Version & Regression Information: This changed between versions 4.6 and 4.7

Code: Playground link with relevant code

const prefix = 'prefix'

// Inferred as an array of specific string when putting the key/value [of an object] with Object.freeze and template string into an array
const freezeWithTplObj = Object.freeze({
    foo: `${prefix}foo`
})
const ary1 = [freezeWithTplObj.foo];
// => ary1: "prefixfoo"[]
// [Transl. Note] Used to be string[] prior to 4.7

// [Inferred as] a string array with only template string
const tplObj = {
    foo: `${prefix}foo`
}
const ary2 = [tplObj.foo];
// => ary2: string[]

// [Inferred as] a string array with only Object.freeze
const freezeObj = Object.freeze({
    foo: "foo"
})
const ary3 = [freezeObj.foo];
// => ary3: string[]

// [Inferred as] a string array with neither Object.freeze nor template string
const obj = {
    foo: "foo"
}
const ary4 = [obj.foo];
// => ary4: string[]

Actual behavior: ary1 inferred as "prefixfoo"[]

Expected behavior: ary1 inferred as string[]

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jul 5, 2022
@RyanCavanaugh
Copy link
Member

This was a requested behavior for Object.freeze to mimic the behavior you get when writing

const tplConst = {
    foo: `${prefix}foo`
} as const;

@RyanCavanaugh
Copy link
Member

e.g. #10725 (comment)

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants