#How come this works in the playground but not in my ide?

1 messages · Page 1 of 1 (latest)

slow sageBOT
#
U9G#0670

Preview:```ts
type ObjectKeys<T> = T extends object
? (keyof T)[]
: T extends number
? []
: T extends Array<any> | string
? string[]
: never

interface ObjectConstructor {
keys_<T>(o: T): ObjectKeys<T>
}

Object.keys_({a: 1})```

scarlet stone
#

I have this in my tsconfig: ```json
{
"compilerOptions": {
"lib": ["ESNext"],
"module": "esnext",
"target": "esnext",
"moduleResolution": "nodenext",
"strict": true,
"downlevelIteration": true,
"skipLibCheck": true,
"jsx": "preserve",
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"allowJs": true,
"types": [
"bun-types" // add Bun global
],
"resolveJsonModule": true
}
}

#

However

#

but only when it's in the middle of a file

#

when it's in it's own file it works fine

#

here, it doesnt work for some reason?

slow sageBOT
#
U9G#0670

Preview:```ts
type ObjectKeys<T> = T extends object
? (keyof T)[]
: T extends number
? []
: T extends Array<any> | string
? string[]
: never

interface ObjectConstructor {
keys_<T>(o: T): ObjectKeys<T>
}

Object.keys_({a: 1})

export type Split<
S extends string,
D
...```

viscid furnace
#

@scarlet stone Files without import or export are global modifying scripts.

scarlet stone
scarlet stone
#

Okay, I see

#

so deleting my tsconfig.json allows this to compile

#
type ObjectKeys<T> = T extends object
    ? (keyof T)[]
    : T extends number
    ? []
    : T extends Array<any> | string
    ? string[]
    : never

interface ObjectConstructor {
    keys_<T>(o: T): ObjectKeys<T>
}

const b = Object.keys_({ a: 1 })
#

but with this tsconfig, it didn't compile

#
{
    "compilerOptions": {
        "lib": ["ESNext"],
        "module": "esnext",
        "target": "esnext",
        "moduleResolution": "nodenext",
        "strict": true,
        "downlevelIteration": true,
        "skipLibCheck": true,
        "jsx": "preserve",
        "allowSyntheticDefaultImports": true,
        "forceConsistentCasingInFileNames": true,
        "allowJs": true,
        "types": [
            "bun-types" // add Bun global
        ],
        "resolveJsonModule": true
    }
}
viscid furnace
#

@scarlet stone You can put the code in a non-module file - or use a declare global block. I don't really think this behavior is particularly tsc-related.

#

FWIW, modifying Object.prototype is generally considered not a great idea - maybe you should just put this in a utility function and import it?

scarlet stone
viscid furnace
#

@scarlet stone FWIW, that's not actually a 'better' type - the built-in type is the correct, safe type. (Though the unsafe type is useful, too)

#

!*:unsafe-keys

slow sageBOT
#
Retsam19#2505
`!retsam19:unsafe-keys`:

Since TS allows objects to have extra properties not specified in the type, it doesn't assume that all the keys on the type are the only keys on the object. This means that Object.keys returns string[] not a specific type, and for(const key in obj), key is string, (not keyof typeof obj).

If you wish to assume otherwise, this utility is often helpful:

// A signature for `Object.keys` that assumes the only keys are the ones indicated by the type
const unsafeKeys = Object.keys as <T>(obj: T) => Array<keyof T>;
scarlet stone
#

Otherwise I have to scavenge for these types